- Digits
- 3, so exponent 3
- Sum
- 1³ + 5³ + 3³ = 1 + 125 + 27 = 153
- Verdict
- Equals
n- Armstrong.
Check Armstrong Number in Java
What you’ll learn
- The Armstrong (narcissistic) condition in base 10: sum of each digit raised to the number of digits.
- A precise algorithm, pseudocode, and two Java programs: one number and a range scan.
- Why integer exponentiation is safer than
Math.powfor exact checks, plus edge cases and complexity. - A browser live preview that mirrors the same digit-power rule.
Overview
An Armstrong number equals the sum of its decimal digits, each raised to the power of how many digits it has. Classic example: 153 = 1³ + 5³ + 3³. This tutorial shows how to implement that test in Java, then scan a range.
Two programs
A single-value check (try 153) and a range listing (from 1 to 200 in the sample output).
Live preview
See digit count k, the expanded sum of each dk, and the verdict without compiling.
Interview polish
Guards for n <= 0, integer powers, overflow notes, and complexity in one place.
Prerequisites
Comfort with loops, integer division, and the modulo operator % is enough to follow the code.
- Java basics:
class Main,public static void main(String[] args), methods, andSystem.out.println. - Extracting the last digit with
n % 10and shifting withn / 10. - Optional: comparing this idea to amicable and abundant problems (different rules, same “loop over structure of
n” pattern).
What is an Armstrong number?
Let n be a positive integer with k decimal digits. Write those digits as dk-1 ... d1 d0 (most significant to least). Then n is an Armstrong number (in base 10) when
dk-1k + ... + d1k + d0k = n.
The exponent is always the digit count, not the digit value. We stay in decimal on this page.
Mathematical definition
Fix base b = 10. If n has k decimal digits ak-1, ..., a0 (most significant to least), then n is Armstrong when ak-1k + ... + a0k = n.
For 100 <= n <= 999, write n = 100a + 10b + c with digits a,b,c. Armstrong means a³ + b³ + c³ = 100a + 10b + c.
Examples: 153, 370, 371, 407 are the only three-digit Armstrong numbers besides the one-digit cases.
Intuition and examples
Count how many digits n has. Peel digits off from the right with % 10 and / 10, raising each digit to that count and adding into a running total. If the total lands back on n, you have an Armstrong number.
Each card shows the digit-power sum with the same exponent k everywhere.
- Digits
- 3, exponent 3
- Sum
- 1³ + 2³ + 3³ = 1 + 8 + 27 = 36
- Verdict
- 36 != 123 - not Armstrong.
- Digits
- 1, exponent 1
- Sum
- 7¹ = 7
- Verdict
- Every 1-9 works the same way.
Takeaway: the exponent is the length of n in decimal.
Live preview
Type a positive integer n. The widget counts decimal digits k, builds the sum of each digit to the kth power, and compares it to n. Caps at 999 999 999 to keep browser arithmetic predictable.
- Try 153, 123, or a single digit.
- Press Run check (or Enter).
- Read the expanded sum line and the verdict.
Algorithm
Goal: decide whether a given n is an Armstrong number in base 10.
Validate n
If n <= 0, return false (or follow your problem’s convention explicitly).
Count digits k
Copy n to a temporary variable and repeatedly divide by 10 until it becomes 0, counting iterations.
Accumulate digit powers
Walk digits again with % 10 and / 10. Add digit^k to a running sum using exact integer exponentiation.
Compare
If the sum equals the original n, return true; otherwise false.
List Armstrong numbers in [low, high]
Loop i from low to high and print each i that passes the same test. The core check does not change.
📜 Pseudocode
function digitCount(n):
k ← 0
t ← n
while t > 0:
k ← k + 1
t ← floor(t / 10)
return k
function sumDigitPowers(n, k):
sum ← 0
t ← n
while t > 0:
d ← t mod 10
sum ← sum + (d to the power k) // integer power
t ← floor(t / 10)
return sum
function isArmstrong(n):
if n <= 0:
return false
k ← digitCount(n)
return sumDigitPowers(n, k) = nCheck a single number (program with explanation)
Uses a small integer power helper so every step stays in integer arithmetic - no floating-point rounding.
Explanation
Two passes over the digits: first to learn k, second to sum dk for each digit d.
if (number <= 0) return false;Guard non-positive inputs. This page uses the common convention of checking positive integers only.
while (t > 0) { k++; t /= 10; }Count digits. Integer division by 10 removes one digit per step.
sum += ipow(d, k);Same exponent for every digit. That exponent is exactly the digit count k.
return sum == number;Final comparison against the original value.
Armstrong numbers in a range
Same isArmstrong helper as Example 1, wrapped in a loop from 1 to 200.
public class Main {
static int ipow(int base, int exp) {
int r = 1;
for (int i = 0; i < exp; i++) {
r *= base;
}
return r;
}
static boolean isArmstrong(int number) {
if (number <= 0) {
return false;
}
int k = 0;
int t = number;
while (t > 0) {
k++;
t /= 10;
}
t = number;
int sum = 0;
while (t > 0) {
int d = t % 10;
sum += ipow(d, k);
t /= 10;
}
return sum == number;
}
public static void main(String[] args) {
int start = 1;
int end = 200;
System.out.println("Armstrong numbers in the range " + start + " to " + end + ":");
for (int i = start; i <= end; i++) {
if (isArmstrong(i)) {
System.out.print(i + " ");
}
}
System.out.println();
}
}Optimization
Exponentiation. For fixed digit count k, precompute 0^k ... 9^k in an array of length 10 and replace ipow(d,k) with a lookup when scanning many numbers.
Bounds on sums. For a given k, the maximum digit-power sum is 9^k * k. This can help prune candidates in generators.
Wide integers. Promote the accumulator to long if powers can overflow int.
Interview: state the O(log n) digit pass clearly; mention lookup tables only if asked.
❓ FAQ
🔄 Input / output examples
For the single-number program with a literal number, typical lines look like this. For interactive runs you can read with Scanner.
Value of number | Typical line printed |
|---|---|
| 153 | 153 is an Armstrong number. |
| 123 | 123 is not an Armstrong number. |
| 7 | 7 is an Armstrong number. |
| 0 | 0 is not an Armstrong number. (with the sample guard) |
For the range program with start = 1 and end = 200:
Armstrong numbers in the range 1 to 200:
1 2 3 4 5 6 7 8 9 153Edge cases and pitfalls
Most bugs come from miscounting digits, reusing a mutated copy of n, or mixing floating-point powers into an integer test.
n <= 0
Return false early. Otherwise 0 can slip through with a digit count of 0 in naive loops.
Losing the original n
Keep one variable for comparison and separate temporaries for counting and summing.
Math.pow rounding
Casting a floating-point power to int can be off by 1 for some values. Prefer integer exponentiation for exact equality.
Partial sums
Digit powers add up quickly. Use long when constraints are large.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
Single isArmstrong(n) (two digit passes, k = O(log n)) | O(log n) | O(1) |
ipow(d, k) per digit (naive multiply loop) | O(k) per call | O(1) |
All n in [1, U] with naive ipow | roughly O(U * log^2 U) | O(1) |
Same range with a fixed table for 0..9 to the power k | O(U log U) | O(1) table |
Auxiliary memory beyond the call stack is a handful of integers in both sample programs.
Summary
- Definition: sum of each decimal digit raised to the digit-count power equals
n. - Implementation: count digits, accumulate integer powers, compare to the original
n; guardn <= 0if required. - Watch-outs: avoid floating-point rounding, watch overflow on the sum, and keep the original value for the final equality.
Besides the trivial one-digit cases 1–9, the only three-digit Armstrong numbers are 153, 370, 371, and 407. For example, 1³ + 5³ + 3³ = 153.
9 people found this page helpful
