Java Program to Find Strong Number with Output and Explanation
if (sum == number) System.out.println("Strong number"); else System.out.println("Not a strong number");.Examples
How to Think About It
Algorithm
Code
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
Initialize variables
number = 145, temp = 145, sum = 0
Extract last digit
digit = 145 % 10 = 5
Calculate factorial of digit
factorial(5) = 120
Add factorial to sum
sum = 0 + 120 = 120
Remove last digit
temp = 145 / 10 = 14
Repeat for next digit
digit = 14 % 10 = 4, factorial(4) = 24, sum = 120 + 24 = 144, temp = 14 / 10 = 1
Repeat for last digit
digit = 1 % 10 = 1, factorial(1) = 1, sum = 144 + 1 = 145, temp = 1 / 10 = 0
Compare sum with original number
sum = 145, number = 145, sum == number is true
| Digit | Factorial | Sum after addition |
|---|---|---|
| 5 | 120 | 120 |
| 4 | 24 | 144 |
| 1 | 1 | 145 |
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
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); } }
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"); } } }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Iterative factorial | O(d) | O(1) | Simple and clear code |
| Recursive factorial | O(d) | O(1) | Elegant code but slower for large inputs |
| Precompute factorials | O(d) | O(1) | Fastest for repeated checks |