-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArmStrong.java
56 lines (52 loc) · 982 Bytes
/
ArmStrong.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
//Write a Program to check the given number is ARMSTRONG or not?
import java.util.Scanner; //371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371
public class ArmStrong
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a num: ");
int num=sc.nextInt();
boolean arm=isArmstrong(num);
if(arm)
System.out.println("armstrong number");
else
System.out.println("not a armstrong number");
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;
}
}