-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArmstrongCheck.java
More file actions
39 lines (33 loc) · 1.02 KB
/
ArmstrongCheck.java
File metadata and controls
39 lines (33 loc) · 1.02 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
// IMPORTANT: class name must be same as file name.
// EG: FILE.java then
// class FILE {
// }
public class ArmstrongCheck {
public static void main(String[] args) {
String input = args[0];
int num = Integer.parseInt(input);
int original = num, sum = 0, digits = 0, temp = num;
while (temp != 0) {
temp /= 10;
digits++;
}
temp = num;
while (temp != 0) {
int digit = temp % 10;
int power = 1;
for (int i = 0; i < digits; i++) {
power *= digit;
}
sum += power;
temp /= 10;
}
if (original == sum) {
System.out.println(num + " is an Armstrong number.");
} else {
System.out.println(num + " is not an Armstrong number.");
}
}
}
//To execute run following in command line.
//java ArmstrongCheck.java <number>
//java ArmstrongCheck.java 153