0
0
JavaProgramBeginner · 2 min read

Java Program to Find Strong Number with Output and Explanation

A Java program to find a strong number calculates the sum of factorials of each digit and compares it with the original number using code like if (sum == number) System.out.println("Strong number"); else System.out.println("Not a strong number");.
📋

Examples

Input145
Output145 is a Strong number
Input123
Output123 is not a Strong number
Input1
Output1 is a Strong number
🧠

How to Think About It

To find if a number is strong, break it into digits, find the factorial of each digit, add these factorials, and check if the sum equals the original number. If yes, it is a strong number.
📐

Algorithm

1
Get the input number.
2
Initialize sum to 0.
3
Extract each digit from the number.
4
Calculate factorial of the digit and add to sum.
5
Compare sum with the original number.
6
Print if it is a strong number or not.
💻

Code

java
public class StrongNumber {
    public static void main(String[] args) {
        int number = 145, temp = number, sum = 0;
        while (temp > 0) {
            int digit = temp % 10;
            sum += factorial(digit);
            temp /= 10;
        }
        if (sum == number) {
            System.out.println(number + " is a Strong number");
        } else {
            System.out.println(number + " is not a Strong number");
        }
    }
    static int factorial(int n) {
        int fact = 1;
        for (int i = 1; i <= n; i++) {
            fact *= i;
        }
        return fact;
    }
}
🔍

Dry Run

Let's trace the number 145 through the code

1

Initialize variables

number = 145, temp = 145, sum = 0

2

Extract last digit

digit = 145 % 10 = 5

3

Calculate factorial of digit

factorial(5) = 120

4

Add factorial to sum

sum = 0 + 120 = 120

5

Remove last digit

temp = 145 / 10 = 14

6

Repeat for next digit

digit = 14 % 10 = 4, factorial(4) = 24, sum = 120 + 24 = 144, temp = 14 / 10 = 1

7

Repeat for last digit

digit = 1 % 10 = 1, factorial(1) = 1, sum = 144 + 1 = 145, temp = 1 / 10 = 0

8

Compare sum with original number

sum = 145, number = 145, sum == number is true

DigitFactorialSum after addition
5120120
424144
11145
💡

Why This Works

Step 1: Extract digits

We use temp % 10 to get each digit from right to left.

Step 2: Calculate factorial

For each digit, we calculate factorial using a loop multiplying numbers from 1 to digit.

Step 3: Sum and compare

We add all factorials and check if the sum equals the original number to decide if it is strong.

🔄

Alternative Approaches

Recursive factorial
java
public class StrongNumber {
    public static void main(String[] args) {
        int number = 145, temp = number, sum = 0;
        while (temp > 0) {
            int digit = temp % 10;
            sum += factorial(digit);
            temp /= 10;
        }
        if (sum == number) {
            System.out.println(number + " is a Strong number");
        } else {
            System.out.println(number + " is not a Strong number");
        }
    }
    static int factorial(int n) {
        if (n == 0 || n == 1) return 1;
        return n * factorial(n - 1);
    }
}
Uses recursion for factorial calculation, which is elegant but may be slower for large digits.
Precompute factorials
java
public class StrongNumber {
    public static void main(String[] args) {
        int[] fact = {1,1,2,6,24,120,720,5040,40320,362880};
        int number = 145, temp = number, sum = 0;
        while (temp > 0) {
            int digit = temp % 10;
            sum += fact[digit];
            temp /= 10;
        }
        if (sum == number) {
            System.out.println(number + " is a Strong number");
        } else {
            System.out.println(number + " is not a Strong number");
        }
    }
}
Stores factorials of digits 0-9 in an array for faster lookup, improving performance.

Complexity: O(d) time, O(1) space

Time Complexity

The program loops through each digit once, and factorial calculation for digits 0-9 is constant time, so overall O(d) where d is number of digits.

Space Complexity

Uses a few integer variables and optionally a small array for factorials, so O(1) constant space.

Which Approach is Fastest?

Precomputing factorials is fastest since it avoids repeated factorial calculations, while recursion is elegant but slower.

ApproachTimeSpaceBest For
Iterative factorialO(d)O(1)Simple and clear code
Recursive factorialO(d)O(1)Elegant code but slower for large inputs
Precompute factorialsO(d)O(1)Fastest for repeated checks
💡
Use a helper method to calculate factorial to keep your code clean and reusable.
⚠️
Forgetting to reset the temporary variable or sum before processing the number causes wrong results.