-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay 69 Armstrong Number
More file actions
40 lines (33 loc) · 1.04 KB
/
Copy pathDay 69 Armstrong Number
File metadata and controls
40 lines (33 loc) · 1.04 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
36
37
38
39
40
public class LearnCoding2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter lower and upper ranges : ");
int low = sc.nextInt();
int high = sc.nextInt();
System.out.println("Armstrong numbers between "+ low + " and " + high + " are : ");
// loop for finding and printing all armstrong numbers between given range
for(int num = low ; num <= high ; num++)
{
int len = getOrder(num);
if(num == getArmstrongSum(num, len))
System.out.print(num + " ");
}
}
private static int getOrder(int num) {
int len = 0;
while (num!=0)
{
len++;
num = num/10;
}
return len;
}
private static int getArmstrongSum(int num, int order) {
if(num == 0)
return 0;
int digit = num % 10;
return (int) Math.pow(digit, order) + getArmstrongSum(num/10, order);
}
}