-
Notifications
You must be signed in to change notification settings - Fork 68
/
Armstrong
36 lines (25 loc) · 955 Bytes
/
Armstrong
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
// Java Code to guess if a number is Armstrong or not
/* An Armstrong number is a number that is a number that is
equal to the sum of its own digits raised to the number of digits.
*/
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int numDigits = String.valueOf(number).length(); // get number of digits in the number
int sum = 0;
int tempNumber = number;
while (tempNumber > 0) {
int digit = tempNumber % 10;
sum += Math.pow(digit, numDigits);
tempNumber /= 10;
}
if (sum == number) {
System.out.println(number + " is an Armstrong number.");
} else {
System.out.println(number + " is not an Armstrong number.");
}
}
}