-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram19.java
More file actions
29 lines (24 loc) · 1015 Bytes
/
Program19.java
File metadata and controls
29 lines (24 loc) · 1015 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
// Program 19
// Week 1 - Day 4
// Armstrong Number: A number is called an Armstrong number if the sum of its digits raised to the power of the number of digits is equal to the number itself. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.
import java.util.Scanner;
public class Program19 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
int originalNumber = number;
int sum = 0;
int digits = String.valueOf(number).length();
while (number > 0) {
int digit = number % 10;
sum += Math.pow(digit, digits);
number /= 10;
}
if (sum == originalNumber) {
System.out.println(originalNumber + " is an Armstrong number.");
} else {
System.out.println(originalNumber + " is not an Armstrong number.");
}
}
}