-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathArmstrongNumber.java
More file actions
46 lines (39 loc) · 1.13 KB
/
ArmstrongNumber.java
File metadata and controls
46 lines (39 loc) · 1.13 KB
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
/*
* ArmstrongNumber.java
* Program to check if a given number is an Armstrong Number
*
* Example:
* Input: 153
* Output: 153 is an Armstrong Number
*
* Compilation:
* javac ArmstrongNumber.java
*
* Run:
* java ArmstrongNumber
*/
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input from user
System.out.print("Enter a number: ");
int num = sc.nextInt();
int originalNumber = num;
int digits = String.valueOf(num).length(); // Number of digits in the number
int sum = 0;
// Calculate the sum of digits raised to the power of total digits
while (num > 0) {
int digit = num % 10;
sum += Math.pow(digit, digits);
num /= 10;
}
// Check if sum equals the original number
if (sum == originalNumber) {
System.out.println(originalNumber + " is an Armstrong Number");
} else {
System.out.println(originalNumber + " is Not an Armstrong Number");
}
sc.close();
}
}