Introduction
An Armstrong number (also known as a narcissistic number) is one of the most popular Java programming interview questions because it combines several basic programming concepts into one problem. To solve it, you need to know how to count digits, extract digits using the modulus (%) and division (/) operators, and raise numbers to a power.
Many beginners learn only the classic 3-digit solution, which works for numbers like 153, but silently fails for Armstrong numbers with more digits. In this guide, you'll first learn the traditional 3-digit implementation, then build a fully generalized solution that works for numbers of any length. You'll also learn how to print Armstrong numbers within a range, implement the logic recursively, understand how Java executes the program internally, and prepare for common interview questions.
What Is an Armstrong Number?
An Armstrong number is a number whose value is equal to the sum of each of its digits raised to the power of the total number of digits.
For example:
153
This number has 3 digits, so each digit is raised to the power 3.
1³ + 5³ + 3³
= 1 + 125 + 27
= 153
Since the calculated value equals the original number, 153 is an Armstrong number.
The important point is that the exponent depends on the number of digits.
Examples:
| Number | Digits | Calculation | Armstrong? |
|---|---|---|---|
| 153 | 3 | 1³ + 5³ + 3³ = 153 | ✅ Yes |
| 370 | 3 | 3³ + 7³ + 0³ = 370 | ✅ Yes |
| 1634 | 4 | 1⁴ + 6⁴ + 3⁴ + 4⁴ = 1634 | ✅ Yes |
| 9474 | 4 | 9⁴ + 4⁴ + 7⁴ + 4⁴ = 9474 | ✅ Yes |
Method 1: Checking a 3-Digit Armstrong Number
This is the traditional version taught in most beginner tutorials.
Java Program
public class ArmstrongThreeDigit {
public static void main(String[] args) {
int num = 153;
int original = num;
int sum = 0;
while (num != 0) {
int digit = num % 10;
sum = sum + (digit * digit * digit);
num = num / 10;
}
if (sum == original) {
System.out.println(original + " is an Armstrong number.");
} else {
System.out.println(original + " is not an Armstrong number.");
}
}
}
Output
153 is an Armstrong number.
Step-by-Step Trace (num = 153)
| Iteration | num (before) | digit | digit³ | sum | num (after) |
|---|---|---|---|---|---|
| 1 | 153 | 3 | 27 | 27 | 15 |
| 2 | 15 | 5 | 125 | 152 | 1 |
| 3 | 1 | 1 | 1 | 153 | 0 |
Since:
sum = 153
original = 153
the number is an Armstrong number.
Limitation
This solution works only for 3-digit numbers because it always cubes each digit.
For example, 1634 is an Armstrong number, but this program incorrectly reports it as not being one because it calculates:
1³ + 6³ + 3³ + 4³
instead of
1⁴ + 6⁴ + 3⁴ + 4⁴
Method 2: General Armstrong Number (Works for Any Number of Digits)
A proper solution first determines the number of digits dynamically.
Java Program
public class ArmstrongGeneral {
public static void main(String[] args) {
int num = 1634;
int original = num;
int temp = num;
int digitCount = String.valueOf(num).length();
int sum = 0;
while (temp != 0) {
int digit = temp % 10;
sum += (int) Math.pow(digit, digitCount);
temp = temp / 10;
}
if (sum == original) {
System.out.println(original + " is an Armstrong number.");
} else {
System.out.println(original + " is not an Armstrong number.");
}
}
}
Output
1634 is an Armstrong number.
Why This Version Works
Unlike Method 1:
-
digit count is calculated dynamically.
-
every digit is raised to the correct power.
-
the program works for 3-digit, 4-digit, 5-digit, and larger Armstrong numbers.
Examples handled correctly:
153
370
371
407
1634
8208
9474
54748
Method 3: Printing Armstrong Numbers in a Range
Interviewers often extend the problem by asking you to print all Armstrong numbers within a range.
Java Program
public class ArmstrongInRange {
static boolean isArmstrong(int num) {
int digitCount = String.valueOf(num).length();
int temp = num;
int sum = 0;
while (temp != 0) {
int digit = temp % 10;
sum += (int) Math.pow(digit, digitCount);
temp = temp / 10;
}
return sum == num;
}
public static void main(String[] args) {
int start = 1;
int end = 10000;
System.out.println("Armstrong numbers:");
for (int num = start; num <= end; num++) {
if (isArmstrong(num)) {
System.out.print(num + " ");
}
}
}
}
Output
Armstrong numbers:
1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474
Notice that every single-digit number is automatically an Armstrong number because:
7¹ = 7
9¹ = 9
Method 4: Using Recursion
The digit-processing loop can also be implemented recursively.
Java Program
public class ArmstrongRecursion {
static int sumOfPowers(int num, int digitCount) {
if (num == 0) {
return 0;
}
int digit = num % 10;
return (int) Math.pow(digit, digitCount)
+ sumOfPowers(num / 10, digitCount);
}
public static void main(String[] args) {
int num = 9474;
int digitCount = String.valueOf(num).length();
int sum = sumOfPowers(num, digitCount);
if (sum == num) {
System.out.println(num + " is an Armstrong number.");
} else {
System.out.println(num + " is not an Armstrong number.");
}
}
}
Output
9474 is an Armstrong number.
Each recursive call processes one digit and returns its contribution to the final sum.
How Java Handles This Internally
Methods 1–3
The following variables are primitive integers stored on the stack:
-
num -
temp -
sum -
digit -
digitCount
The statement:
String.valueOf(num)
creates a temporary String object on the heap only to calculate the number of digits.
Math.pow()
Math.pow() always returns a double.
For that reason, we cast it back to an integer:
(int) Math.pow(digit, digitCount)
For the small integer powers used in Armstrong numbers, this conversion is generally safe.
Recursion
Each recursive call creates a new stack frame containing:
-
current number
-
current digit
-
digit count
The calls return one by one until the total sum is produced.
Real-Life Analogy
Imagine dismantling a machine into its individual parts.
Each part is modified according to a fixed rule.
Finally, all modified parts are assembled again.
Usually, the result is a completely different machine.
Very rarely, the modified parts recreate the original machine exactly.
That rare situation is exactly what an Armstrong number is—its transformed digits reconstruct the original number.
Comparison Table
| Method | Works for All Digit Counts? | Best Used When |
|---|---|---|
| Hardcoded Cube | ❌ No | Learning the basic idea |
| Dynamic Digit Count | ✅ Yes | Interviews and production code |
| Range-Based Method | ✅ Yes | Printing Armstrong numbers |
| Recursion | ✅ Yes | Demonstrating recursion |
Best Practices
-
Never hardcode the exponent as 3.
-
Calculate the digit count dynamically.
-
Preserve the original number before extracting digits.
-
Compute the digit count only once.
-
Extract the Armstrong logic into a reusable
isArmstrong()method. -
Remember that all single-digit numbers are Armstrong numbers.
Common Mistakes
Hardcoding Cubes
Incorrect:
digit * digit * digit
Correct:
Math.pow(digit, digitCount)
Losing the Original Number
Always preserve:
int original = num;
before modifying num.
Confusing Armstrong and Perfect Numbers
Armstrong numbers are based on:
sum of digit powers
Perfect numbers are based on:
sum of proper divisors
They are completely different concepts.
Forgetting Single-Digit Numbers
Numbers:
0
1
2
...
9
are all Armstrong numbers because every digit raised to the power 1 equals itself.
Miscalculating the Digit Count
Avoid hardcoding:
power = 3;
Always calculate the number of digits dynamically.
Expert Tips
A strong interview explanation is:
"An Armstrong number is one where the sum of each digit raised to the power of the total number of digits equals the original number. I first determine the digit count dynamically, then extract every digit using the modulus operator, raise it to that power with
Math.pow(), sum the results, and compare the total with the original number. This approach works for numbers of any length, unlike the commonly taught 3-digit-only solution."
Mentioning why you avoid hardcoding cubes is a strong signal that you understand the complete problem rather than only the textbook example.
Pros and Cons
| Method | Advantages | Disadvantages |
|---|---|---|
| Hardcoded Cube | Simple and easy to understand | Works only for 3-digit numbers |
| Dynamic Digit Count | Correct for every digit length | Slightly longer implementation |
| Recursion | Elegant and reusable | Additional stack overhead |
Frequently Asked Questions
What is an Armstrong number?
A number whose digits, raised to the power of the total number of digits, sum back to the original number.
Example:
153
1³ + 5³ + 3³ = 153
Why doesn't cubing every digit always work?
Because the exponent depends on the total number of digits.
For example:
1634
requires:
1⁴ + 6⁴ + 3⁴ + 4⁴
not cubes.
How do I write a general Armstrong number program?
Calculate the digit count first and use:
Math.pow(digit, digitCount)
for every digit.
Are all single-digit numbers Armstrong numbers?
Yes.
Every digit raised to the power of 1 equals itself.
Common Armstrong Numbers
1
2
3
4
5
6
7
8
9
153
370
371
407
1634
8208
9474
What's the difference between an Armstrong number and a Perfect number?
Armstrong numbers depend on digit powers.
Perfect numbers depend on the sum of proper divisors.
Can I solve this using recursion?
Yes.
Each recursive call processes one digit and returns its powered value.
What is the time complexity?
O(d)
where d is the number of digits.
Each digit is processed exactly once.
Is this a common interview question?
Yes.
Interviewers often expect candidates to move beyond the basic 3-digit implementation and produce a solution that works for numbers of any length.
How do I print Armstrong numbers within a range?
Loop through every number in the range and call a reusable isArmstrong() method.
Why is Math.pow() preferred?
Because the exponent changes depending on the number of digits, and Math.pow() handles any exponent without writing separate multiplication logic.
Are Armstrong numbers also called narcissistic numbers?
Yes.
Both terms describe the same mathematical property.