Java Program to Check Armstrong Number between Two Integers

Last Updated : 5 Aug, 2026

An Armstrong number (also called a Narcissistic number) is a positive integer that is equal to the sum of its own digits, where each digit is raised to the power of the total number of digits in the number. In this program, we check every number within a given range and print all the Armstrong numbers.

  • Every number in the given range is checked individually.
  • Examples of Armstrong numbers are 153, 370, 371, 407, 1634, etc.

Illustration

Input: start = 100, end = 200
Output: 153

Input: start = 100, end = 500
Output: 153 370 371 407

Formula

If a number has digits a, b, c, ...and contains n digits, then it is an Armstrong number if:

Number = aâŋ + bâŋ + câŋ + ...

Example:

Input: 100 200
Output: 153


Explanation : 100 and 200 are given two integers.
153 = 1*1*1 + 5*5*5 + 3*3*3
= 1 + 125 + 27
= 153
Therefore, only 153 is an Armstrong number between 100 and 200.

Algorithm

  • Read the starting and ending values of the range.
  • Traverse every number between the given limits.
  • Count the total number of digits in the current number.
  • Calculate the sum of each digit raised to the power of the digit count.
  • If the calculated sum equals the original number, print it as an Armstrong number.

Approach

  • Traverse all numbers between the given range.
  • For each number: Count the number of digits (n) & Compute the sum of each digit raised to the power n.
  • If the calculated sum equals the original number, it is an Armstrong number.
  • Print the number.
Java
class GFG {

    // Function to print Armstrong numbers
    static void armstrongNumbers(int start, int end) {

        for (int num = start; num <= end; num++) {

            int original = num;
            int temp = num;

            // Count number of digits
            int digits = 0;
            while (temp != 0) {
                digits++;
                temp /= 10;
            }

            temp = num;
            int sum = 0;

            // Calculate sum of digits raised to power 'digits'
            while (temp != 0) {
                int digit = temp % 10;
                sum += (int) Math.pow(digit, digits);
                temp /= 10;
            }

            // Check Armstrong number
            if (sum == original) {
                System.out.print(original + " ");
            }
        }
    }

    public static void main(String[] args) {

        int start = 100;
        int end = 500;

        System.out.print("Armstrong numbers: ");
        armstrongNumbers(start, end);
    }
}

Output
Armstrong numbers: 153 370 371 407 

Explanation: The program checks each number in the given range one by one. For every number, it first counts the number of digits, then calculates the sum of each digit raised to that count using Math.pow(). If the calculated sum matches the original number, it is printed as an Armstrong number.

Comment