Java ProgramsNumbersPrint Armstrong Numbers in a Range

Print Armstrong Numbers in a Range in Java

beginner·  Numbers  ·  Number Theory

Problem

An Armstrong number is a number equal to the sum of its own digits, each raised to the power of the total digit count — printing them in a range means checking every candidate one by one.

Given a range of numbers, print every Armstrong number it contains.

Input
1 to 1000
Output
Armstrong numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407

Java Program

Java
public class ArmstrongRange { static boolean isArmstrong(int num) { int digitCount = String.valueOf(num).length(); int original = num; int sum = 0; while (num > 0) { int digit = num % 10; sum += (int) Math.pow(digit, digitCount); // raise this digit to the power of the digit count num /= 10; } return sum == original; } public static void main(String[] args) { StringBuilder result = new StringBuilder(); for (int i = 1; i <= 1000; i++) { if (isArmstrong(i)) { // reuse the single-number check for every candidate if (result.length() > 0) result.append(", "); result.append(i); } } System.out.println("Armstrong numbers: " + result); } }

Output

Armstrong numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407

Core Logic

Testing every number in the range with the same digit-power-sum check used to test a single number finds every Armstrong number at once.

How It Works
  1. 1isArmstrong(n) is the single-number check, pulled out into its own method so it can be reused for every candidate.
  2. 2The main loop tries every number from 1 to 1000, calling isArmstrong() on each.
  3. 3A number that passes the check is appended to the result, separated by commas.
  4. 4Every single-digit number from 1 to 9 automatically qualifies, since any single digit raised to the power of 1 is itself.
Scanning 1 to 1000 finds the nine single digits, then 153, 370, 371, and 407 — the only multi-digit Armstrong numbers in that range.
💡

Key Point: Reusing isArmstrong() as a standalone method avoids duplicating the digit-power-sum logic between the single-number checker and this range scanner.

Complexity
Time Complexity: O(n × d)Space Complexity: O(1)

Why: Every one of the n candidates in the range gets its own O(d) digit check, where d is that number's digit count.

Key Concepts

helper methodfor loopdigit extraction

Approach 2: Java 8

Java
import java.util.stream.Collectors; import java.util.stream.IntStream; public class ArmstrongRangeStream { static boolean isArmstrong(int num) { int digitCount = String.valueOf(num).length(); int sum = String.valueOf(num).chars() .map(c -> (int) Math.pow(c - '0', digitCount)) .sum(); return sum == num; } public static void main(String[] args) { // Keeps only the numbers that pass the Armstrong check, then joins them String result = IntStream.rangeClosed(1, 1000) .filter(ArmstrongRangeStream::isArmstrong) .mapToObj(String::valueOf) .collect(Collectors.joining(", ")); System.out.println("Armstrong numbers: " + result); } }

Output

Armstrong numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407

Core Logic

The same range scan can filter a stream of candidate numbers down to just the Armstrong ones, then join the survivors.

How It Works
  1. 1IntStream.rangeClosed(1, 1000) generates every candidate number in the range.
  2. 2.filter(ArmstrongRangeStream::isArmstrong) keeps only the numbers that pass the Armstrong check, reusing the same helper method as a method reference.
  3. 3.mapToObj(String::valueOf) converts each surviving number into a String.
  4. 4.collect(Collectors.joining(", ")) joins them into the final comma-separated result.
Filtering 1 through 1000 keeps the same thirteen numbers the loop version finds, in the same order.
💡

Key Point: Passing ArmstrongRangeStream::isArmstrong as a method reference to filter() is what lets the exact same check be reused without rewriting it as a lambda.

Complexity
Time Complexity: O(n × d)Space Complexity: O(n)

Why: The stream still checks every candidate's digits, and Collectors.joining() builds a result string holding every Armstrong number found.

Key Concepts

StreamIntStream.rangeClosed()filter()

Related Programs