forked from dharmanshu1921/Daa-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharmstrong_number.java
More file actions
39 lines (29 loc) · 912 Bytes
/
armstrong_number.java
File metadata and controls
39 lines (29 loc) · 912 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
33
34
35
36
37
38
39
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;
}
}