easyNumber ProblemsJava

Print All Armstrong Numbers Between 1 and 1000

Print every Armstrong number in the range 1 to 1000 using the sum-of-cubes test.

Quick Answer

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

Example 1
Input:  1 to 200
Output: 1 153

1 = 1^3 and 153 = 1^3+5^3+3^3; the next Armstrong number 370 is above 200.

Example 2
Input:  1 to 1000
Output: 1 153 370 371 407

Each equals the sum of the cubes of its digits within the range.

Constraints

  • The scan range is 1 to the given limit, inclusive
  • The 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
Write a helper that returns true when a number equals the sum of the cubes of its digits.
Hint 2
Extract digits with `% 10` and `/ 10`, cubing each and accumulating the sum.
Hint 3
Loop the number from 1 to the limit and print each value the helper accepts.

Approach

Reveal the step-by-step approach

Separate the per-number test from the range scan.

  1. Write isArmstrong(n): extract each digit, add digit^3 to a running sum, and return whether that sum equals n.
  2. Loop n from 1 to the limit.
  3. Collect every n for which isArmstrong(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.

Time: O(m * d) for limit m and digit count d per numberSpace: O(1) beyond the output

Common 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 →
Chat with us