-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy patharmstrong_number.java
49 lines (43 loc) · 1.39 KB
/
armstrong_number.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.Scanner;
public class ArmstrongNumber {
public static boolean isArmstrongNumber(int n) {
int numDigits = String.valueOf(n).length();
int sumOfPowers = 0;
int temp = n;
while (temp > 0) {
int digit = temp % 10;
sumOfPowers += Math.pow(digit, numDigits);
temp /= 10;
}
return n == sumOfPowers;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
scanner.close();
if (isArmstrongNumber(num)) {
System.out.println(num + " is an Armstrong number.");
} else {
System.out.println(num + " is not an Armstrong number.");
}
}
}
//-----------------------------------------------------------------------------------
public class ArmstrongNumber {
public static boolean isArmstrongNumber(int n) {
int numDigits = String.valueOf(n).length();
int sumOfPowers = 0;
int temp = n;
while (temp > 0) {
int digit = temp % 10;
sumOfPowers += Math.pow(digit, numDigits);
temp /= 10;
}
return n == sumOfPowers;
}
public static void main(String[] args) {
System.out.println(isArmstrongNumber(153) ? "True" : "False");
}
}
// Output: True