-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP18_StrongNumber.java
More file actions
40 lines (33 loc) · 1.25 KB
/
Copy pathP18_StrongNumber.java
File metadata and controls
40 lines (33 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package programs;
/**
* ============================================================
* PROGRAM 18: Strong (Krishnamurthy / Special) Number
* ============================================================
* Problem: WAP to check if a number is a Strong Number.
* - A number is Strong if the sum of the factorials of its digits
* equals the number itself.
* - Example: 145 = 1! + 4! + 5! = 1 + 24 + 120 = 145 ✓
* ============================================================
*/
public class P18_StrongNumber {
private static final int[] FACTORIAL_LOOKUP = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
public static boolean isStrongNumber(int n) {
if (n <= 0) return false;
int original = n;
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += FACTORIAL_LOOKUP[digit];
n /= 10;
}
return sum == original;
}
public static void main(String[] args) {
int[] testCases = {1, 2, 145, 40585, 123, 500};
System.out.println("=== STRONG NUMBER CHECKER ===");
for (int num : testCases) {
System.out.printf(" %5d -> %s%n",
num, isStrongNumber(num) ? "✓ STRONG NUMBER" : "✗ Not Strong");
}
}
}