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

Java Basic Tutorial

Java Process 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 Output (I/O)

Java Reader/Writer

Java other topics

Java program to check Armstrong numbers

Java Examples Comprehensive

In this program, you will learn to check if a given number is an Armstrong number. You will learn how to do this by using for loop and while loop in Java.

A positive integer is called an Armstrong order, n 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  // 153 is an Armstrong number.

Example1Check if Armstrong is a number with3digits

public class Armstrong {
    public static void main(String[] args) {
        int number = 371, originalNumber, remainder, result = 0;
        originalNumber = number;
        while (originalNumber != 0)
        {
            remainder = originalNumber % 10;
            result += Math.pow(remainder, 3);
            originalNumber /= 10;
        }
        if (result == number)
            System.out.println(number + " is an Armstrong number.");
        else
            System.out.println(number + " is not an Armstrong number.");
    }
}

When running the program, the output is:

371 is an Armstrong number.
  • First, the value of the given number (number) is stored in another integer variable originalNumber. This is because, we need to compare the final number with the original number's value in the end.

  • Then, use a while loop to traverse originalNumber until it equals 0.

    • In each iteration, the last digit of num is stored in remainder.

    • Then, use the Math.pow() function to add remainder to3(number of digits)and add to result.

    • Then, divide by10Then, remove the last digit from originalNumber.

  • Finally, compare result and number. If they are equal, it is an Armstrong number. If not, it is not.

Example2Check if Armstrong is a number with n digits

public class Armstrong {
    public static void main(String[] args) {
        int number = 1634, originalNumber, remainder, result = 0, n = 0;
        originalNumber = number;
        for (; originalNumber != 0; originalNumber /= 10, ++n);
        originalNumber = number;
        for (; originalNumber != 0; originalNumber /= 10)
        {
            remainder = originalNumber % 10;
            result += Math.pow(remainder, n);
        }
        if (result == number)
            System.out.println(number + " is an Armstrong number.");
        else
            System.out.println(number + " is not an Armstrong number.");
    }
}

When running the program, the output is:

1634 is an Armstrong number.

In this program, we did not use the while loop, but instead used two for loops.

The first for loop is used to calculate the number of digits in the number. It is in the following compressed form:

for (; originalNumber != 0; originalNumber /= 10) {
     n++;
}

Then, the second for loop calculates result, where the remainder is raised to the power of n in each iteration.

Access this page to learn howDisplay all Armstrong numbers between two intervals.

Java Examples Comprehensive