-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRangeArmstrong.java
58 lines (52 loc) · 881 Bytes
/
RangeArmstrong.java
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
55
56
57
58
//Write a Program to display the range of ARMSTRONG numbers?
import java.util.Scanner;
public class RangeArmstrong {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a range: ");
int r=sc.nextInt();
for(int x=0;x<=r;x++)
{
boolean arm=isArmstrong(x);
if(arm)
System.out.println(x);
}
sc.close();
}
private static boolean isArmstrong(int num) {
int count = CountNum(num);
int temp=num;
int sum=0;
while(temp>0)
{
int z=temp%10;
sum=sum+pwr(z,count);
temp=temp/10;
}
if(sum==num)
return true;
else
return false;
}
private static int pwr(int z, int count)
{
int pw=1;
while(count>0)
{
pw=pw*z;
count--;
}
return pw;
}
private static int CountNum(int num)
{
int count=0;
while(num>0)
{
count++;
num=num/10;
}
return count;
}
}