-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindarm.java
More file actions
42 lines (35 loc) · 980 Bytes
/
findarm.java
File metadata and controls
42 lines (35 loc) · 980 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
//find armstrong number between 1 to n
import java.util.Scanner;
public class findarm {
public static boolean isArmstrong(int n) {
int copyofN = n;
int numberOfDigits = 0;
while (n > 0) {
numberOfDigits++;
n = n / 10;
}
int sum = 0;
n = copyofN;
while (n > 0) {
int lastDigit = n % 10;
sum += Math.pow(lastDigit, numberOfDigits);
n = n / 10;
}
if (sum == copyofN) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int start = sc.nextInt();
int end = sc.nextInt();
for (int i = start; i <= end; i++) {
if (isArmstrong(i) == true) {
System.out.println(i + " ");
}
}
}
}