-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArmstrongNumberinRange.java
More file actions
35 lines (30 loc) · 1.1 KB
/
Copy pathArmstrongNumberinRange.java
File metadata and controls
35 lines (30 loc) · 1.1 KB
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
import java.util.Scanner;
class ArmstrongNumberinRange {
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
System.out.println("Range : Low & High ");
int low = sc.nextInt();
int high = sc.nextInt();
System.out.println("Armstrong Numbers between "+low+" & "+high+" are :");
for(int number = low + 1; number < high; ++number) {
int digits = 0;
int result = 0;
int originalNumber = number;
// number of digits calculation
while (originalNumber != 0) {
originalNumber /= 10;
++digits;
}
originalNumber = number;
// 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 == number) {
System.out.print(number + " ");
}
}
}
}