-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArmstrong
More file actions
32 lines (26 loc) · 954 Bytes
/
Copy pathArmstrong
File metadata and controls
32 lines (26 loc) · 954 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
30
31
32
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number to check if it's an Armstrong number: ");
int number = sc.nextInt();
int originalNumber, remainder, result = 0, n = 0;
originalNumber = number;
// Finding the number of digits
while (originalNumber != 0) {
originalNumber /= 10;
++n;
}
originalNumber = number;
// Calculating the sum of nth power of digits
while (originalNumber != 0) {
remainder = originalNumber % 10;
result += Math.pow(remainder, n);
originalNumber /= 10;
}
if (result == number)
System.out.println(number + " is an Armstrong number.");
else
System.out.println(number + " is not an Armstrong number.");
}
}