-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArmstrongNumber.java
More file actions
53 lines (36 loc) · 1.17 KB
/
ArmstrongNumber.java
File metadata and controls
53 lines (36 loc) · 1.17 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
41
42
43
44
45
46
47
48
49
50
51
52
53
package strings;
public class ArmstrongNumber {
static void checkArmstrongNumber(int number)
{
int tempNumber = number;
int noOfDigits = String.valueOf(number).length();
int sum = 0;
while (tempNumber != 0)
{
int lastDigit = tempNumber % 10;
int lastDigitToThePowerOfNoOfDigits = 1;
for(int i = 0; i < noOfDigits; i++)
{
lastDigitToThePowerOfNoOfDigits = lastDigitToThePowerOfNoOfDigits * lastDigit;
}
sum = sum + lastDigitToThePowerOfNoOfDigits;
tempNumber = tempNumber / 10;
}
if (sum == number)
{
System.out.println(number+" is an armstrong number");
}
else
{
System.out.println(number+" is not an armstrong number");
}
}
public static void main(String[] args) {
checkArmstrongNumber(153);
checkArmstrongNumber(371);
checkArmstrongNumber(9474);
checkArmstrongNumber(54748);
checkArmstrongNumber(407);
checkArmstrongNumber(1674);
}
}