-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram21.java
More file actions
36 lines (29 loc) · 1.12 KB
/
Program21.java
File metadata and controls
36 lines (29 loc) · 1.12 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
// Program 21
// Week 1 - Day 4
// A number can be said as a stong number when the sum of the factorial of the individual digits of the number is equal to the number itself. For example, 145 is a strong number since 1! + 4! + 5! = 145. Similarly, 40585 is a strong number since 4! + 0! + 5! + 8! + 5! = 40585.
import java.util.Scanner;
public class Program21 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int originalNumber = number;
int sum = 0;
while (number > 0) {
int digit = number % 10;
sum += factorial(digit);
number /= 10;
}
if (sum == originalNumber) {
System.out.println(originalNumber + " is a Strong number.");
} else {
System.out.println(originalNumber + " is not a Strong number.");
}
}
public static int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
}