-
Notifications
You must be signed in to change notification settings - Fork 1
/
21_armstrong-2.java
45 lines (35 loc) · 947 Bytes
/
21_armstrong-2.java
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
43
44
45
public class Armstrong
{
public static void main(String[] args)
{
int low = 999, high = 99999;
for(int number = low + 1; number < high; ++number)
{
if (checkArmstrong(number))
System.out.print(number + " ");
}
}
public static boolean checkArmstrong(int num)
{
int digits = 0;
int result = 0;
int originalNumber = num;
// number of digits calculation
while (originalNumber != 0)
{
originalNumber /= 10;
++digits;
}
originalNumber = num;
// result contains sum of nth power of its digits
while (originalNumber != 0)
{
int remainder = originalNumber % 10;
result += Math.pow(remainder, digits);
originalNumber /= 10;
}
if (result == num)
return true;
return false;
}
}