-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathArmstrong.java
More file actions
32 lines (28 loc) · 847 Bytes
/
Armstrong.java
File metadata and controls
32 lines (28 loc) · 847 Bytes
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
// filename: Armstrong.java
// Compile: javac Armstrong.java
// Run: java Armstrong 153
public class Armstrong {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Usage: java Armstrong <number>");
return;
}
int num = Integer.parseInt(args[0]);
int original = num, sum = 0, digits = 0;
int temp = num;
while (temp > 0) {
digits++;
temp /= 10;
}
temp = num;
while (temp > 0) {
int digit = temp % 10;
sum += Math.pow(digit, digits);
temp /= 10;
}
if (sum == original)
System.out.println(original + " is an Armstrong number.");
else
System.out.println(original + " is not an Armstrong number.");
}
}