AbishekThese are the most important beginner problems asked in interviews and tests. Let’s understand them...
These are the most important beginner problems asked in interviews and tests.
Let’s understand them with logic + flow + code in 3 languages.
Find the sum from a starting number to an ending number.
output
import java.util.Scanner;
public class SumN {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int start = sc.nextInt();
int end = sc.nextInt();
int result = 0;
while (start <= end) {
result += start;
start++;
}
System.out.println(result);
}
}
let start = 1;
let end = 10;
let result = 0;
while (start <= end) {
result += start;
start++;
}
console.log(result);
Find the smallest number divisible by both numbers.
output
import java.util.Scanner;
public class LCM {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int max = (a > b) ? a : b;
while (true) {
if (max % a == 0 && max % b == 0) {
System.out.println(max);
break;
}
max++;
}
}
}
let a = 30;
let b = 35;
let max = a > b ? a : b;
while (true) {
if (max % a === 0 && max % b === 0) {
console.log(max);
break;
}
max++;
}
Find factorial of a number
5! = 5 × 4 × 3 × 2 × 1 = 120
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int result = 1;
while (n > 0) {
result *= n;
n--;
}
System.out.println(result);
}
}
let n = 5;
let result = 1;
while (n > 0) {
result *= n;
n--;
}
console.log(result);