-
Notifications
You must be signed in to change notification settings - Fork 0
/
armstrong.java
33 lines (28 loc) · 1020 Bytes
/
armstrong.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
import java.util.Scanner;
public class armstrong {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int originalNumber = num; // Store the original number
int digits = 0;
// Count the number of digits
while (originalNumber != 0) {
originalNumber /= 10;
digits++;
}
originalNumber = num; // Reset originalNumber to the input number
int result = 0;
// Calculate the sum of nth power of individual digits
while (originalNumber != 0) {
int remainder = originalNumber % 10;
result += Math.pow(remainder, digits);
originalNumber /= 10;
}
// Check if the sum is equal to the original number
if (result == num) {
System.out.println("Armstrong Number");
} else {
System.out.println("Not An Armstrong Number");
}
}
}