Print All Armstrong Numbers Between 1 and 1000
Print every Armstrong number in the range 1 to 1000 using the sum-of-cubes test.
In this classic range, an Armstrong number equals the sum of the cubes of its digits. Scan every number in the range and keep the ones where that holds. Between 1 and 1000 the Armstrong numbers are 1, 153, 370, 371 and 407.
Problem Statement
Print every Armstrong number in the range 1 to a limit (classically
1000). In this three-digit-oriented form, a number is Armstrong when it equals
the sum of the cubes of its digits.
For example, 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153, so 153 is Armstrong.
The full list between 1 and 1000 is 1, 153, 370, 371, 407.
Input: An inclusive upper limit (here 1000).
Output: The Armstrong numbers in the range, in ascending order, separated by spaces.
Examples
Input: 1 to 200
Output: 1 1531 = 1^3 and 153 = 1^3+5^3+3^3; the next Armstrong number 370 is above 200.
Input: 1 to 1000
Output: 1 153 370 371 407Each equals the sum of the cubes of its digits within the range.
Constraints
The scan range is 1 to the given limit, inclusiveThe test uses the sum of the cubes of the digits
Think Before You Code
Reveal the questions to ask yourself first
- What single-number test decides whether a value is Armstrong here?
- How do you compute the sum of the cubes of the digits without strings?
- How do you drive that test across the whole range?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Separate the per-number test from the range scan.
- Write
isArmstrong(n): extract each digit, adddigit^3to a running sum, and return whether that sum equalsn. - Loop
nfrom1to the limit. - Collect every
nfor whichisArmstrong(n)is true.
Keeping the helper separate makes the logic easy to test and reuse.
Dry Run
Walk through the example step by step
Testing whether 153 is Armstrong:
step | num | digit | digit^3 | sum
-----+-----+-------+---------+-----
1 | 153 | 3 | 27 | 27
2 | 15 | 5 | 125 | 152
3 | 1 | 1 | 1 | 153
end | 0 | — | — | 153
sum (153) == 153 -> Armstrong, so it is printed.
Solution
Reveal the full Java solution
public class ArmstrongNumbers {
public static String armstrongUpTo(int limit) {
StringBuilder sb = new StringBuilder();
for (int n = 1; n <= limit; n++) {
if (isArmstrong(n)) {
if (sb.length() > 0) {
sb.append(" ");
}
sb.append(n);
}
}
return sb.toString();
}
private static boolean isArmstrong(int n) {
int num = n;
int sum = 0;
while (num != 0) {
int digit = num % 10;
sum += digit * digit * digit;
num /= 10;
}
return sum == n;
}
public static void main(String[] args) {
System.out.println(armstrongUpTo(200)); // 1 153
System.out.println(armstrongUpTo(1000)); // 1 153 370 371 407
}
}
The isArmstrong helper peels each digit with % 10, cubes it, and adds it to a
running sum, then compares that sum with the original number. Splitting the test
from the loop keeps each part simple: the loop only decides the range, and the
helper only decides membership. This sum-of-cubes definition is the standard one
used for the 1-to-1000 exercise.
O(m * d) for limit m and digit count d per numberSpace: O(1) beyond the outputCommon Mistakes
- Passing the original n into the digit loop and then comparing the mutated value.
- Using the number of digits as the exponent (general narcissistic rule) instead of a fixed cube here.
Edge Cases to Test
- 1 is Armstrong (1^3 = 1) and is the only single-digit member with the cube rule.
- 1000 itself is not Armstrong (1^3+0+0+0 = 1), so the inclusive bound is safe.
Interview Follow-Ups
- How would you generalise to true narcissistic numbers, raising each digit to the digit count?
- How would you extend the range to four-digit Armstrong numbers?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →