Strong Number

Java coding interview problem for Number Logic: Strong Number.

Checking whether a number is a Strong Number is a popular Java coding interview question.

This problem tests your understanding of:

  • Loops
  • Factorial
  • Digit extraction
  • Arithmetic operators
  • Mathematical logic
  • Problem-solving skills

Strong Number problems are commonly asked in Java, C, C++, Python, and competitive programming interviews.


What is a Strong Number?

A Strong Number is a number whose sum of the factorials of its digits is equal to the original number.

In simple words,

Strong Number

=

Sum of Factorials of Digits

Mathematical Definition

If a number contains digits

d1

d2

d3

...

Then

d1!

+

d2!

+

d3!

+

...

=

Original Number

What is Factorial?

The factorial of a number is the product of all positive integers from 1 to that number.

Examples

0! = 1

1! = 1

2! = 2

3! = 6

4! = 24

5! = 120

6! = 720

7! = 5040

8! = 40320

9! = 362880

Example 1

Number

145

Digits

1

4

5

Factorials

1!

=

1
4!

=

24
5!

=

120

Sum

1 + 24 + 120

=

145

Since

145 = 145

Therefore

145 is a Strong Number

Example 2

Number

2

Factorial

2!

=

2

Therefore

2 is a Strong Number

Example 3

Number

123

Digits

1

2

3

Factorials

1! = 1

2! = 2

3! = 6

Sum

1 + 2 + 6

=

9

Since

9 ≠ 123

Therefore

123 is NOT a Strong Number

Some Strong Numbers

Number Strong Number?
1 ✅ Yes
2 ✅ Yes
145 ✅ Yes
40585 ✅ Yes
123 ❌ No
100 ❌ No
200 ❌ No

Real Interview Question

Write a Java program to check whether a given number is a Strong Number.


Understanding the Logic

Suppose

Number = 145

Extract each digit.

Calculate its factorial.

Add all factorial values.

Compare the sum with the original number.

If both are equal,

the number is a Strong Number.


Visual Representation

Input

145

Processing

145

↓

Digit = 5

↓

5! = 120

↓

Digit = 4

↓

4! = 24

↓

Digit = 1

↓

1! = 1

↓

Total

↓

120 + 24 + 1

↓

145

↓

Compare

↓

145 == 145

↓

Strong Number

Brute Force Approach

The simplest solution is

  • Store the original number.
  • Extract one digit at a time.
  • Find the factorial of the digit.
  • Add the factorial to the sum.
  • Remove the last digit.
  • Continue until the number becomes zero.
  • Compare the sum with the original number.

Algorithm

Step 1

Read the number.

Step 2

Store the original number.

original = number;

Step 3

Initialize

sum = 0;

Step 4

Extract the last digit.

digit = number % 10;

Step 5

Find the factorial of the digit.

Step 6

Add the factorial to the sum.

Step 7

Remove the last digit.

number = number / 10;

Step 8

Repeat until

number = 0

Step 9

Compare

sum == original

Dry Run

Input

145

Initial

sum = 0
Digit Factorial Running Sum
5 120 120
4 24 144
1 1 145

Comparison

145 == 145

Output

Strong Number

Another Dry Run

Input

123

Initial

sum = 0
Digit Factorial Running Sum
3 6 6
2 2 8
1 1 9

Comparison

9 ≠ 123

Output

Not a Strong Number

Approach 1 — Using Iteration (Loop)

This is the most common interview solution and is easy for beginners to understand.


Complete Java Program

public class StrongNumber {

    public static void main(String[] args) {

        int number = 145;

        int original = number;

        int sum = 0;

        while (number > 0) {

            int digit = number % 10;

            int factorial = 1;

            for (int i = 1; i <= digit; i++) {

                factorial *= i;

            }

            sum += factorial;

            number /= 10;

        }

        if (sum == original) {

            System.out.println(original + " is a Strong Number");

        } else {

            System.out.println(original + " is NOT a Strong Number");

        }

    }

}

Output

145 is a Strong Number

Step-by-Step Code Explanation

Step 1

Declare the number.

int number = 145;

Current value

145

Step 2

Store the original value.

int original = number;

The original number is needed because the value of

number

changes while extracting digits.


Step 3

Initialize the sum.

int sum = 0;

Initially

sum = 0

Step 4

Extract the last digit.

int digit = number % 10;

For

145

First digit extracted

5

Step 5

Find the factorial.

int factorial = 1;

for (int i = 1; i <= digit; i++) {

    factorial *= i;

}

For

5

Calculation

1 × 2 × 3 × 4 × 5

=

120

Step 6

Add the factorial.

sum += factorial;

Running sum

120

↓

144

↓

145

Step 7

Remove the last digit.

number /= 10;

Values become

145

↓

14

↓

1

↓

0

Step 8

Compare

sum == original

If true,

the number is a Strong Number.

Otherwise,

it is not.


Example Execution

Input

40585

Digits

4

0

5

8

5

Factorials

24

1

120

40320

120

Sum

40585

Output

40585 is a Strong Number

Input

200

Digits

2

0

0

Factorials

2

1

1

Sum

4

Output

200 is NOT a Strong Number

Why Does This Work?

The algorithm extracts each digit of the number, calculates its factorial, and adds it to a running total.

After processing every digit, it compares the total with the original number.

If both values are equal, the number satisfies the mathematical definition of a Strong Number.


Advantages of This Approach

  • Easy to understand.
  • Uses simple loops.
  • Demonstrates digit extraction.
  • Good beginner interview problem.
  • No additional data structures required.

Drawbacks

Although this approach works well, it calculates the factorial of every digit repeatedly.

Interviewers often ask follow-up questions such as:

  • Can you avoid recalculating factorials?
  • Can you precompute factorials of digits 0–9?
  • Can you create a reusable factorial method?
  • Can you solve it recursively?
  • What is the time complexity?
  • How can you optimize the solution?

In Part 2, we'll cover:

  • Optimized Approach Using Precomputed Factorials
  • Reusable Factorial Method
  • Recursive Factorial Solution
  • Time and Space Complexity
  • Comparison of Approaches
  • Common Interview Mistakes
  • Interview Follow-up Questions
  • Related Coding Problems
  • Key Takeaways
  • Interview Tips

Approach 2 — Optimized Approach (Precomputed Factorials)

In the previous solution, we calculated the factorial for every digit repeatedly.

Since a digit can only be between

0

and

9

there are only 10 possible factorial values.

Instead of calculating them every time, we can precompute them once and reuse them.

This significantly improves performance.


Why Does This Optimization Work?

The factorial of every digit is constant.

0! = 1

1! = 1

2! = 2

3! = 6

4! = 24

5! = 120

6! = 720

7! = 5040

8! = 40320

9! = 362880

Instead of repeatedly calculating

5!

=

1 × 2 × 3 × 4 × 5

we simply retrieve

factorial[5]

=

120

Visual Representation

Digit

↓

5

↓

factorial[5]

↓

120

↓

Add to Sum

Instead of

5!

↓

1×2×3×4×5

↓

120

Algorithm

Step 1

Create an array containing factorials of digits

0–9

Step 2

Extract each digit.

Step 3

Retrieve its factorial directly.

Step 4

Add it to the sum.

Step 5

Compare the sum with the original number.


Java Program

public class StrongNumberOptimized {

    public static void main(String[] args) {

        int number = 145;

        int original = number;

        int sum = 0;

        int[] factorial = {
                1,
                1,
                2,
                6,
                24,
                120,
                720,
                5040,
                40320,
                362880
        };

        while (number > 0) {

            int digit = number % 10;

            sum += factorial[digit];

            number /= 10;

        }

        if (sum == original) {

            System.out.println(original + " is a Strong Number");

        } else {

            System.out.println(original + " is NOT a Strong Number");

        }

    }

}

Output

145 is a Strong Number

Approach 3 — Using a Reusable Factorial Method

Instead of writing factorial logic inside the loop, create a separate method.

Benefits

  • Cleaner code
  • Easy to reuse
  • Easy to unit test

Java Program

public class StrongNumberMethod {

    static int factorial(int number) {

        int result = 1;

        for (int i = 1; i <= number; i++) {

            result *= i;

        }

        return result;

    }

    static boolean isStrong(int number) {

        int original = number;

        int sum = 0;

        while (number > 0) {

            int digit = number % 10;

            sum += factorial(digit);

            number /= 10;

        }

        return sum == original;

    }

    public static void main(String[] args) {

        int number = 40585;

        if (isStrong(number)) {

            System.out.println(number + " is a Strong Number");

        } else {

            System.out.println(number + " is NOT a Strong Number");

        }

    }

}

Output

40585 is a Strong Number

Approach 4 — Recursive Factorial

The factorial can also be calculated recursively.


Java Program

public class StrongNumberRecursion {

    static int factorial(int n) {

        if (n == 0 || n == 1) {

            return 1;

        }

        return n * factorial(n - 1);

    }

    public static void main(String[] args) {

        int number = 145;

        int original = number;

        int sum = 0;

        while (number > 0) {

            int digit = number % 10;

            sum += factorial(digit);

            number /= 10;

        }

        if (sum == original) {

            System.out.println("Strong Number");

        } else {

            System.out.println("Not a Strong Number");

        }

    }

}

Output

Strong Number

Time Complexity

Basic Iterative Solution

Operation Complexity
Time O(d × 9) ≈ O(d)
Space O(1)

where

d

is the number of digits.


Precomputed Factorials

Operation Complexity
Time O(d)
Space O(1)

This is the preferred interview solution.


Recursive Factorial

Operation Complexity
Time O(d × 9)
Space O(9)

Extra stack space is used during recursion.


Comparison of Approaches

Approach Time Space Recommended
Iterative Factorial O(d) O(1) Good for Beginners
Precomputed Factorials O(d) O(1) ✅ Best for Interviews
Reusable Method O(d) O(1) Production Ready
Recursive Factorial O(d) O(9) Good for Learning

Common Mistakes

Mistake 1

Forgetting that

0! = 1

Wrong

0! = 0

Correct

0! = 1

Mistake 2

Not storing the original number.

Wrong

number

gets modified while extracting digits.

Correct

int original = number;

Mistake 3

Using

sum = factorial;

instead of

sum += factorial;

Always accumulate the factorials.


Mistake 4

Extracting digits incorrectly.

Correct

digit = number % 10;

Remove the digit

number /= 10;

Mistake 5

Recalculating factorial repeatedly.

Instead of

factorial(5)

factorial(5)

factorial(5)

Use

factorial[5]

Interview Follow-up Questions

Q1. What is a Strong Number?

Q2. Why are only digits 0–9 involved?

Q3. Why is precomputing factorials faster?

Q4. Can you solve this recursively?

Q5. Print all Strong Numbers in a range.

Q6. What is the difference between Strong Number and Armstrong Number?

Q7. Why is 145 a Strong Number?

Q8. Can negative numbers be Strong Numbers?

Q9. Compare iterative and recursive factorial implementations.

Q10. How would you optimize this for repeated checks?


Related Coding Problems

  • Armstrong Number
  • Perfect Number
  • Factorial
  • Sum of Digits
  • Reverse Number
  • Palindrome Number
  • Neon Number
  • Harshad Number

Key Takeaways

  • A Strong Number is equal to the sum of the factorials of its digits.
  • The most common Strong Numbers are:
1

2

145

40585
  • Digit extraction is performed using:
digit = number % 10;
  • Remove the processed digit using:
number /= 10;
  • Precomputing factorials for digits 0–9 avoids repeated calculations and is the preferred interview optimization.
  • The optimized solution runs in O(d) time, where d is the number of digits, and uses O(1) extra space.

Interview Tip

If an interviewer asks:

"Write a Java program to check whether a number is a Strong Number."

Begin with the straightforward loop-based solution that extracts each digit, computes its factorial, and accumulates the result. Once the basic solution is complete, explain that factorial values for digits 0–9 never change and can be precomputed in an array to eliminate repeated calculations. Mention that this optimization improves performance while keeping the implementation simple. Also discuss handling edge cases such as single-digit numbers (1 and 2), and explain the difference between Strong Numbers and Armstrong Numbers, as interviewers often ask this as a follow-up.