Product of Digits Easy
Problem
Given a number, return the product of its digits. (Variants ask you to keep multiplying until a single digit remains.)
The idea, plainly
Peel one digit at a time off the right-hand end: n % 10 gives you the last digit, n / 10 removes it. Multiply as you go. This digit-peel loop is the backbone of half of TCS's math questions.
Brute force
Convert to a String and multiply each character. Works, but allocates and TCS sometimes bans built-ins.
public static int productOfDigits(int n) {
String s = String.valueOf(Math.abs(n));
int p = 1;
for (int i = 0; i < s.length(); i++) {
p *= (s.charAt(i) - '0');
}
return p;
}Optimal
Pure arithmetic — no String allocation.
public static int productOfDigits(int n) {
n = Math.abs(n);
if (n == 0) return 0;
int p = 1;
while (n > 0) {
p *= n % 10; // take the last digit
n /= 10; // drop the last digit
}
return p;
}
// Variant: keep going until one digit is left
public static int productUntilSingle(int n) {
while (n > 9) n = productOfDigits(n);
return n;
}The trap
Watch n = 0 (the loop never runs, so return 0 explicitly) and any digit that is 0 (makes the whole product 0 — that is correct, not a bug).
Digit Sum (reduce to a single digit) Easy
Problem
Add the digits of a number repeatedly until a single digit remains. e.g. 9875 → 29 → 11 → 2.
The idea, plainly
The loop version is the obvious one. But there is a beautiful closed form: the answer is the digital root, which equals 1 + (n-1) % 9 for n > 0. That is O(1) with no loop at all — a genuinely impressive thing to write in a TCS test.
Brute force
Repeatedly sum the digits until the number is below 10.
public static int digitSum(int n) {
while (n > 9) {
int s = 0;
while (n > 0) { s += n % 10; n /= 10; }
n = s;
}
return n;
}Optimal
Digital root formula — constant time.
public static int digitalRoot(int n) {
if (n == 0) return 0;
return 1 + (n - 1) % 9;
}The trap
Know both. If the question asks to "show the steps", use the loop. If it just wants the final digit, the formula is instant. n = 0 must be special-cased in the formula.
GCD and LCM Easy
Problem
Find the greatest common divisor and least common multiple of two numbers.
The idea, plainly
Euclid's algorithm: the GCD of (a, b) is the same as the GCD of (b, a mod b). Keep replacing until one becomes 0 — the other is your answer. Then LCM comes free: a × b = GCD × LCM.
Brute force
Try every number from min(a,b) downwards until one divides both.
public static int gcd(int a, int b) {
int g = 1;
for (int i = Math.min(a, b); i >= 1; i--) {
if (a % i == 0 && b % i == 0) { g = i; break; }
}
return g;
}Optimal
Euclid — logarithmic.
public static int gcd(int a, int b) {
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
return a;
}
public static long lcm(int a, int b) {
// divide FIRST to avoid int overflow
return (long) a / gcd(a, b) * b;
}The trap
LCM overflows easily. Write a / gcd * b, never a * b / gcd — the second multiplies first and can blow past int range.
Armstrong Number Easy
Problem
A number is an Armstrong number if the sum of its digits each raised to the power of the digit-count equals the number itself. 153 = 1³+5³+3³ ✓
The idea, plainly
Two steps: count the digits, then sum each digit raised to that power. Do not hardcode power 3 — TCS tests 4-digit cases like 1634 to catch exactly that.
Brute force
Using Math.pow on doubles — works but risks floating-point rounding on larger inputs.
public static boolean isArmstrong(int n) {
String s = String.valueOf(n);
int d = s.length(), sum = 0;
for (char c : s.toCharArray()) {
sum += (int) Math.pow(c - '0', d);
}
return sum == n;
}Optimal
Integer arithmetic only — no doubles, no String.
public static boolean isArmstrong(int n) {
int d = 0, tmp = n;
while (tmp > 0) { d++; tmp /= 10; }
int sum = 0;
tmp = n;
while (tmp > 0) {
int dig = tmp % 10;
int p = 1;
for (int i = 0; i < d; i++) p *= dig; // integer power
sum += p;
tmp /= 10;
}
return sum == n;
}The trap
Math.pow returns a double. For big values the cast back to int can be off by one. Use an integer power loop.