Java Program to Check Armstrong Number
Learn how to check if a number is an Armstrong number in Java using digit extraction and power calculation with multiple approaches and examples.
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 example:
- 153 = 1³ + 5³ + 3³ = 1 + 125 + 27 = 153 (3 digits)
- 9474 = 9⁴ + 4⁴ + 7⁴ + 4⁴ = 6561 + 256 + 2401 + 256 = 9474 (4 digits)
Before you start, you may want to review:
1) Basic Armstrong Check
This method extracts digits and calculates the sum of powers.
Example
class Main {
public static void main(String[] args) {
int num = 153; // change value to test
if (isArmstrong(num)) {
System.out.println(num + " is an Armstrong number.");
} else {
System.out.println(num + " is not an Armstrong number.");
}
}
static boolean isArmstrong(int num) {
int original = num;
int digits = countDigits(num);
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum += Math.pow(digit, digits);
num /= 10;
}
return original == sum;
}
static int countDigits(int num) {
if (num == 0) return 1;
int count = 0;
while (num > 0) {
count++;
num /= 10;
}
return count;
}
}Output (for num = 153)
153 is an Armstrong number.2) Optimized Version (Without Math.pow)
Avoid floating-point operations by using integer multiplication.
Example
class Main {
public static void main(String[] args) {
int num = 9474; // change value to test
if (isArmstrongOptimized(num)) {
System.out.println(num + " is an Armstrong number.");
} else {
System.out.println(num + " is not an Armstrong number.");
}
}
static boolean isArmstrongOptimized(int num) {
int original = num;
int digits = countDigits(num);
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum += power(digit, digits);
num /= 10;
}
return original == sum;
}
static int power(int base, int exp) {
int result = 1;
for (int i = 0; i < exp; i++) {
result *= base;
}
return result;
}
static int countDigits(int num) {
if (num == 0) return 1;
int count = 0;
while (num > 0) {
count++;
num /= 10;
}
return count;
}
}Output (for num = 9474)
9474 is an Armstrong number.3) Find Armstrong Numbers in Range
Program to find all Armstrong numbers within a given range.
Example
import java.util.ArrayList;
import java.util.List;
class Main {
public static void main(String[] args) {
int start = 1;
int end = 10000;
System.out.println("Armstrong numbers between " + start + " and " + end + ":");
List<Integer> armstrongNumbers = findArmstrongInRange(start, end);
for (int num : armstrongNumbers) {
System.out.println(num + " (digits: " + countDigits(num) + ")");
}
System.out.println("\nTotal Armstrong numbers found: " + armstrongNumbers.size());
}
static List<Integer> findArmstrongInRange(int start, int end) {
List<Integer> result = new ArrayList<>();
for (int i = start; i <= end; i++) {
if (isArmstrong(i)) {
result.add(i);
}
}
return result;
}
static boolean isArmstrong(int num) {
int original = num;
int digits = countDigits(num);
int sum = 0;
while (num > 0) {
int digit = num % 10;
sum += power(digit, digits);
num /= 10;
}
return original == sum;
}
static int power(int base, int exp) {
int result = 1;
for (int i = 0; i < exp; i++) {
result *= base;
}
return result;
}
static int countDigits(int num) {
if (num == 0) return 1;
int count = 0;
while (num > 0) {
count++;
num /= 10;
}
return count;
}
}Sample Output
Armstrong numbers between 1 and 10000:
1 (digits: 1)
2 (digits: 1)
3 (digits: 1)
4 (digits: 1)
5 (digits: 1)
6 (digits: 1)
7 (digits: 1)
8 (digits: 1)
9 (digits: 1)
153 (digits: 3)
371 (digits: 3)
407 (digits: 3)
1634 (digits: 4)
8208 (digits: 4)
9474 (digits: 4)
Total Armstrong numbers found: 15Notes & Tips
- Common Armstrong numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 371, 407, 1634, 8208, 9474
- Performance: The optimized version using integer multiplication is faster than Math.pow()
- Large numbers: For numbers with many digits, consider using
longorBigIntegerto avoid overflow - Time complexity: O(d) where d is the number of digits
- Space complexity: O(1) for the basic check, O(n) for range finding