-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumOfDigits.java
More file actions
42 lines (35 loc) · 1.01 KB
/
SumOfDigits.java
File metadata and controls
42 lines (35 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
37
38
39
40
41
42
import java.io.*;
public class SumOfDigits {
public static void main() throws IOException {
SumOfDigits obj = new SumOfDigits();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER A NUMBER: ");
int num = Integer.parseInt(br.readLine());
System.out.println("SUM OF DIGITS = " + obj.sumOfDigits(num));
if (obj.factorCheck(num) == 1) {
System.out.println("EQUAL");
} else {
System.out.println("NOT EQUAL");
}
}
public int sumOfDigits(int num) {
int sum = 0;
while (num != 0) {
sum = sum + num % 10;
num = num / 10;
}
return sum;
}
public int factorCheck(int num) {
int fact = 0;
int sum = sumOfDigits(num);
int factorial = 1;
for (int i = 1; i <= sum; i++) {
factorial *= i;
}
if (sum == factorial) {
fact = 1;
}
return fact;
}
}