-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrongNumberCheck.java
More file actions
36 lines (32 loc) · 1.01 KB
/
StrongNumberCheck.java
File metadata and controls
36 lines (32 loc) · 1.01 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
// 31. Write a program to check if a given number is a strong number.
import java.util.Scanner;
public class StrongNumberCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
boolean isStrong = isStrongNumber(num);
if (isStrong) {
System.out.println(num + " is a strong number.");
} else {
System.out.println(num + " is not a strong number.");
}
scanner.close();
}
public static boolean isStrongNumber(int num) {
int originalNum = num;
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum += factorial(digit);
num /= 10;
}
return sum == originalNum;
}
public static int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
}