-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharmstrong.java
More file actions
39 lines (37 loc) · 1.02 KB
/
Copy patharmstrong.java
File metadata and controls
39 lines (37 loc) · 1.02 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
import java.util.*;
class armstrong{
public static void main(String args[]){
/*
* An Armstrong number is a number that is equal to the sum of its own digits
* each raised to the power of the number of digits.
For example:
-> 153=1^3+5^3+3^3 → so 153 is an Armstrong number.
-> 9474=9^4+4^4+7^4+4^4 → so 9474 is also an Armstrong number.
*/
Scanner sc=new Scanner(System.in);
System.out.println("Enter your number");
int n=sc.nextInt();
int n1=n;
int num = n;
int r=0;
int s=0;
int d =0 ;
while(n>0){
d++;
n=n/10;
} while(n1>0){
r=n1%10;
s += (int)Math.pow(r, d);
n1 = n1/10;
}
if(num == s)
{
System.out.println(num +" is an Armstrong Number");
}
else
{
System.out.println(num +" is not an Armstrong Number");
}
sc.close();
}
}