Java Program to Find Armstrong Numbers in a Range

Finding Armstrong numbers within a range is an extension of the basic Armstrong number problem. It helps beginners understand nested loops and number processing in Java.

In this tutorial, we will create a Java program to find all Armstrong numbers between a given start and end range.

By the end of this guide, you will understand how to apply Armstrong logic across multiple values using loops.

Concept Overview

An Armstrong number is equal to the sum of its digits each raised to the power of the number of digits.

We repeat this check for every number in the given range using a loop.

Program

Java
import java.util.Scanner;

public class ArmstrongRange {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter start of range: ");
        int start = sc.nextInt();

        System.out.print("Enter end of range: ");
        int end = sc.nextInt();

        System.out.println("Armstrong numbers in the given range:");

        for (int i = start; i <= end; i++) {
            int num = i;
            int original = num;
            int sum = 0;
            int digits = String.valueOf(num).length();

            while (num != 0) {
                int digit = num % 10;
                sum += Math.pow(digit, digits);
                num = num / 10;
            }

            if (sum == original) {
                System.out.print(original + " ");
            }
        }
    }
}

Output

TEXT
Enter start of range: 1
Enter end of range: 500
Armstrong numbers in the given range:
1 153 370 371 407

Detailed Explanation

The program takes a starting and ending range from the user.

A loop iterates through every number in the range.

For each number, it checks whether it is an Armstrong number.

The digit extraction and power calculation logic is applied inside the loop.

If the condition is satisfied, the number is printed.

Example Walkthrough

Let us consider the range 1 to 500.

The program checks each number and finds Armstrong numbers like 1, 153, 370, 371, and 407.

Applications

This logic is used in number theory problems, coding challenges, and algorithm practice.

Advantages of This Approach

This program helps beginners understand nested loops and repeated logic application.

It demonstrates how a single concept can be applied to multiple inputs.

Limitations

The program may be slow for very large ranges due to repeated calculations.

Improvements You Can Make

You can optimize digit counting without converting to a string.

You can store results in a list for further processing.

This Java program strengthens understanding of loops, conditionals, and number theory concepts.