These are the most important beginner problems asked in interviews and tests.
Let’s understand them with logic + flow + code in 3 languages.
1. Sum of N Numbers
Find the sum from a starting number to an ending number.
Python
output
JAVA
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);
}
}
JavaScript
let start = 1;
let end = 10;
let result = 0;
while (start <= end) {
result += start;
start++;
}
console.log(result);
2. LCM (Least Common Multiple)
Find the smallest number divisible by both numbers.
Python
output
JAVA
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++;
}
}
}
JavaScript
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++;
}
3. Factorial
Find factorial of a number
5! = 5 × 4 × 3 × 2 × 1 = 120
Python
JAVA
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);
}
}
JavaScript
let n = 5;
let result = 1;
while (n > 0) {
result *= n;
n--;
}
console.log(result);





Top comments (0)