Strong Number in Java
A Strong Number is a number in which the sum of the factorial of each digit is equal to the number itself. These numbers are quite rare, and they demonstrate the usefulness of factorial operations in algorithmic problem solving.
What Is a Strong Number?
A number is considered strong if:
Sum of factorials of digits = Original number
For example:
145 → 1! + 4! + 5!
→ 1 + 24 + 120
→ 145 (Strong Number)
So, 145 is a Strong Number because the sum of factorials of its digits gives back the same number.
Examples of Strong Numbers
- 1
- 2
- 145
- 40585
Algorithm to Check Strong Number
- Take input number.
- Extract each digit using modulus
%. - Find factorial of each digit.
- Add all factorial values.
- If sum equals the original number → it is a Strong Number.
Java Program to Check Strong Number
public class StrongNumber {
// Method to find factorial of a digit
static int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
public static void main(String[] args) {
int num = 145;
int temp = num;
int sum = 0;
while (temp > 0) {
int digit = temp % 10;
sum += factorial(digit);
temp /= 10;
}
if (sum == num) {
System.out.println(num + " is a Strong Number.");
} else {
System.out.println(num + " is NOT a Strong Number.");
}
}
}
Output
145 is a Strong Number.
Conclusion
Strong Numbers illustrate how digit-based mathematical patterns can be identified using factorial operations. Though rare, they are a great practice problem for beginners learning loops, digits extraction, and basic algorithms in Java.