-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArmstrongInRange.java
More file actions
36 lines (32 loc) · 860 Bytes
/
Copy pathArmstrongInRange.java
File metadata and controls
36 lines (32 loc) · 860 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
import java.util.*;
public class ArmstrongInRange {
public static int getLen(int num) {
int len = 0;
while (num != 0) {
len++;
num = num / 10;
}
return len;
}
public static int armstrong(int n, int len) {
int res = 0;
int temp = n;
while (temp > 0) {
int rem = temp % 10;
res += Math.pow(rem, len);
temp = temp / 10;
}
return res;
}
public static void main(String args[]) {
int low = 150;
int high = 153;
for (int i = low; i <= high; i++) {
int length = getLen(i);
int result = armstrong(i, length);
if (result == i) {
System.out.println(i + " is Armstrong");
}
}
}
}