-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArmstrongNumbers.java
47 lines (36 loc) · 1.14 KB
/
ArmstrongNumbers.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
import java.util.Scanner;
public class ArmstrongNumbers {
public static void main(String[] args) {
int number, digitnumber = 0, digitvalue = 1, res = 0, digitpow, tempnumber = 0;
boolean asknumber = false;
Scanner input = new Scanner(System.in);
do {
System.out.println("Please enter a number: ");
number = input.nextInt();
if (number > 0 && number <= 999) {
asknumber = true;
}
}
while (!asknumber);
tempnumber = number;
while (tempnumber != 0) {
tempnumber /= 10;
digitnumber++;
}
tempnumber = number;
while (tempnumber != 0) {
digitvalue = tempnumber % 10;
digitpow = 1;
for (int i = 1; i <= digitnumber; i++) {
digitpow *= digitvalue;
}
res += digitpow;
tempnumber /= 10;
}
if (res == number) {
System.out.println(number + " is an Armstrong Number.");
} else {
System.out.println(number + " is not an Armstrong Number.");
}
}
}