-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArmstrong.java
More file actions
40 lines (31 loc) · 879 Bytes
/
Armstrong.java
File metadata and controls
40 lines (31 loc) · 879 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
import java.util.*;
public class Armstrong {
public static boolean isArmstrong(int num){
int original = num;
int sum=0, count =0;
// count number
int temp = num;
while(temp >0){
temp /=10;
count++;
}
//calculate sum of digit
temp = num;
while(temp >0){
int digit = temp % 10;
sum += Math.pow(digit, count);
temp/= 10;
}
return sum == original;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number : ");
int n = sc.nextInt();
if(isArmstrong(n)){
System.out.println(n + " is Armstrong number");
}else{
System.out.println(n + " is not Armstrong number");
}
}
}