Java Problem Solving - Lab 1

Java Problem Solving - Lab 1

4 min read

1. A process for any problem

  1. Understand. Restate the problem in your own words. What is the input? What is the output?

  2. Work examples by hand. Solve 2–3 small examples on paper, including a tricky one (0, a negative number, 1).

  3. Write the steps in plain language (pseudocode). Hindi, English, anything.

  4. Code it, one step at a time, running as you go.

  5. Test, including edge cases.

2. The IntelliJ debugger

Instead of adding print statements everywhere, watch the code run line by line:

  1. Click in the left margin next to a line number to set a breakpoint (a red dot).

  2. Click the bug icon instead of the green run arrow.

  3. The program pauses at the breakpoint. The Variables panel shows every variable's current value.

  4. Step Over (F8) runs the current line and moves to the next. Step Into (F7) goes inside a method call. Resume (F9) runs to the next breakpoint.

3. The digit toolkit

Most number problems use one loop pattern:

Java
while (n > 0) {    int digit = n % 10;   // take the last digit    // ... do something with digit ...    n = n / 10;           // remove the last digit}
The digit-extraction loop on 472.

4. Worked problems

Reverse a number (1234 → 4321)

Java
static int reverse(int n) {    int rev = 0;    while (n > 0) {        int digit = n % 10;        rev = rev * 10 + digit;   // shift rev left, add the new digit        n /= 10;    }    return rev;}

n

digit

rev

1234

4

4

123

3

43

12

2

432

1

1

4321

Palindrome number (121, 1331)

Java
static boolean isPalindrome(int n) {    return n >= 0 && n == reverse(n);}

Reusing reverse is the whole point of methods.

Prime check, and why we stop at √n

If n has a factor bigger than √n, it must also have one smaller than √n (because factors come in pairs: 36 = 4 × 9, where 4 < 6 < 9). So checking up to √n is enough. For n = 1,000,003 this means about 1,000 checks instead of 1,000,000.

Prime check flow.

Fibonacci series (0 1 1 2 3 5 8 ...)

Java
static void printFibonacci(int count) {    long a = 0, b = 1;    for (int i = 0; i < count; i++) {        System.out.print(a + " ");        long next = a + b;        a = b;        b = next;    }    System.out.println();}

GCD with Euclid's algorithm, and LCM

The GCD of a and b equals the GCD of b and a % b, repeating until the remainder is 0.

Java
static int gcd(int a, int b) {    while (b != 0) {        int r = a % b;        a = b;        b = r;    }    return a;}static int lcm(int a, int b) {    return a / gcd(a, b) * b;   // divide first to reduce overflow risk}

gcd(48, 18): (48, 18) → (18, 12) → (12, 6) → (6, 0), so the answer is 6.

Armstrong number (153 = 1³ + 5³ + 3³)

Java
static boolean isArmstrong(int n) {    int digits = String.valueOf(n).length();    int sum = 0, temp = n;    while (temp > 0) {        int d = temp % 10;        sum += (int) Math.pow(d, digits);        temp /= 10;    }    return sum == n;}

Notice temp: we work on a copy so that n is still available for the final comparison. This is a very common pattern.

Interview questions

Why is it enough to check divisors up to the square root when testing for primes?

Divisors come in pairs (d, n/d). If both were greater than √n, their product would exceed n. So any composite number has a divisor ≤ √n.

How do you reverse an integer without converting it to a String?

Repeatedly take n % 10 as the last digit, build rev = rev * 10 + digit, and do n /= 10 until n is 0.

Practice

  1. easy Print all Armstrong numbers between 1 and 1000.

  2. easy Perfect number: the sum of its proper divisors equals the number (6 = 1 + 2 + 3). Check a number, then print all perfect numbers below 10,000.

  3. easy Calculate power(base, exp) using a loop, without Math.pow.

  4. medium Strong number: the sum of the factorials of its digits equals the number (145 = 1! + 4! + 5!).

  5. medium Convert a decimal number to binary (as a String or by printing digits), and binary back to decimal.

  6. medium Keep adding the digits of a number until a single digit remains (9875 → 29 → 11 → 2).

  7. medium Print all prime numbers between two numbers entered by the user, and how many there are.

© 2026 Rohit Parihar. All rights reserved.

RP

Discussion

?