-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patharmstrong.java
More file actions
54 lines (44 loc) · 815 Bytes
/
armstrong.java
File metadata and controls
54 lines (44 loc) · 815 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package lecture3;
import java.util.Scanner;
public class armstrong {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int min = s.nextInt();
int max = s.nextInt();
printArmstrong(min, max);
}
public static void printArmstrong(int n1, int n2) {
while (n1 <= n2) {
boolean b;
b = isArmstrong(n1);
if (b)
System.out.println(n1);
n1++;
}
}
public static boolean isArmstrong(int n) {
boolean b = false;
int c = 0;
c = count(n);
int power = c;
int temp = n;
int sum = 0, rem = 0;
while (c > 0) {
rem = temp % 10;
sum = sum + (int) Math.pow(rem, power);
temp /= 10;
c--;
}
if (sum == n)
b = true;
return b;
}
public static int count(int n) {
int i = 0;
while (n > 0) {
n /= 10;
i++;
}
return i;
}
}