Java Program to Check Armstrong Number
An Armstrong number (also called a narcissistic number) is a number that equals the sum of its digits, each raised to the power of the number of digits.
For a 3-digit number, each digit is cubed. 153 is an Armstrong number because:
1³ + 5³ + 3³ = 1 + 125 + 27 = 153
For a 4-digit number each digit is raised to the power 4: 1634 = 1⁴ + 6⁴ + 3⁴ + 4⁴. All single-digit numbers 0 to 9 are Armstrong numbers, and the 3-digit ones are 153, 370, 371 and 407.
1. Check a 3-Digit Armstrong Number
The loop takes the last digit with % 10, adds its cube to sum, and removes the digit with / 10. A copy of the number is used in the loop so the original value is still available for the final comparison.
public class ArmstrongNumber {
public static void main(String[] args) {
int[] numbers = {153, 370, 537};
for (int number : numbers) {
int sum = 0;
int temp = number;
while (temp > 0) {
int digit = temp % 10;
sum += digit * digit * digit;
temp /= 10;
}
if (sum == number) {
System.out.println(number + " is an Armstrong number.");
} else {
System.out.println(number + " is not an Armstrong number.");
}
}
}
}
153 is an Armstrong number. 370 is an Armstrong number. 537 is not an Armstrong number.
2. Check an Armstrong Number with Any Number of Digits
For numbers of any length, first count the digits, then raise each digit to that power. String.valueOf(n).length() gives the number of digits, and a small loop does the power so the program stays in whole numbers.
import java.util.Scanner;
public class ArmstrongAnyDigits {
static boolean isArmstrong(int number) {
int digits = String.valueOf(number).length();
int sum = 0;
int temp = number;
while (temp > 0) {
int digit = temp % 10;
int power = 1;
for (int i = 0; i < digits; i++) {
power *= digit;
}
sum += power;
temp /= 10;
}
return sum == number;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
scanner.close();
System.out.println(number + (isArmstrong(number) ? " is an Armstrong number." : " is not an Armstrong number."));
}
}
Enter a number: 1634 1634 is an Armstrong number.
Enter a number: 285 285 is not an Armstrong number.
3. Print All Armstrong Numbers Up to 10000
With the check in a method, it is easy to test every number in a range.
public class ArmstrongRange {
static boolean isArmstrong(int number) {
int digits = String.valueOf(number).length();
int sum = 0;
for (int temp = number; temp > 0; temp /= 10) {
sum += (int) Math.pow(temp % 10, digits);
}
return sum == number;
}
public static void main(String[] args) {
System.out.println("Armstrong numbers from 1 to 10000:");
for (int n = 1; n <= 10000; n++) {
if (isArmstrong(n)) {
System.out.print(n + " ");
}
}
System.out.println();
}
}
Armstrong numbers from 1 to 10000: 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474
4. Summary
- An Armstrong number equals the sum of its digits, each raised to the power of the number of digits.
- Extract digits with
% 10and remove them with/ 10, working on a copy of the number. - Count the digits first so the same method works for 3, 4 or more digits.
- The Armstrong numbers up to 10000 are 1–9, 153, 370, 371, 407, 1634, 8208 and 9474.