English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List (List)

Java Queue (queue)

Java Map collection

Java Set collection

Java input and output (I/O)

Java Reader/Writer

Java other topics

Java program to display Armstrong numbers between two intervals

Comprehensive Java Examples

In this program, you will learn how to display all Armstrong numbers between two given intervals (low and high) in Java.

A positive integer is called an n-order Armstrong number if

abcd... = an + bn + cn + dn + ...

For3digit Armstrong number, the sum of the cubes of each digit equals the number itself. For example:

153 = 1*1*1 + 5*5*5 + 3*3*3  // 153is an Armstrong number.

This program is based onHow to check if an integer is an Armstrong numberconcept.

Example: Armstrong numbers between two integers

public class Armstrong {
    public static void main(String[] args) {
        int low = 999, high = 99999;
        for(int number = low + 1; number < high; ++number) {
            int digits = 0;
            int result = 0;
            int originalNumber = number;
            //Digit Calculation
            while (originalNumber != 0) {
                originalNumber /= 10;
                ++digits;
            }
            originalNumber = number;
            //The result is the sum of the nth powers of its digits
            while (originalNumber != 0) {
                int remainder = originalNumber % 10;
                result += Math.pow(remainder, digits);
                originalNumber /= 10;
            }
            if (result == number)
                System.out.print(number + "");
        }
    }
}

When running the program, the output is:

1634 8208 9474 54748 92727 93084

In the above program, each number between the given high and low intervals was checked.

After each check, digits and result will be restored to 0.

Comprehensive Java Examples