Java Program to Check Armstrong Number

In this post, we will discuss what is Armstrong number and also create java program to check if the given number is an Armstrong number or not.

Armstrong Number:

An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits.
For example, 153 is an Armstrong number because 13 + 53 + 33 = 153.

Problem :

We are given a number. And we use java program to find whether number is an Armstrong Number or not.

Here Example of the problem:

Example 1:
Input :  Enter a number: 407
Output:  407 is an Armstrong number.
Example 2 :
Input : Enter a number: 8202
Output: 8202 is an Armstrong number.
Example 3 :
Input : Enter a number: 123 
Output: 123 is not an Armstrong number.

Explanation:

In example 1, the user entered the number 407. The program calculated 43+03+73. Since the result matches the original number, the program outputs that 407 is an Armstrong number.

In example 2, the user entered the number 8202. The program calculated 84+24+ 04+ 84=8208. Check if the sum equals the original number: 8208 == 8208.  The result matches the original number, the program outputs that 1634 is an Armstrong number.

In example 3, the user the number 123. The program calculated13+23+33=36, which does not match the original number (123). Since the result is not equal to the original number, the program outputs that 123 is not an Armstrong number.

Algorithm :

 Initialize variables. 

 Use a loop to calculate the sum of the cubes of each digit.

 Check if the total is equal to the original number. 

 Print the result.

This java program checks if a number is an Armstrong number.

public class ArmstrongNumber {

    public static void main(String[] args) {

        int num = 370, number, temp, total = 0;

        number = num;
        while (number != 0)
        {
            temp = number % 10;
            total = total + temp*temp*temp;
            number /= 10;
        }

        if(total == num)
            System.out.println(num + " is an Armstrong number");
        else
            System.out.println(num + " is not an Armstrong number");
    }
}

Output :

370 is an Armstrong number

Here’s how it works: –

We start by initializing variables: “”num” as the number that we want to check,

“number” as a copy of num,

“temp” as a temporary variable, and “total” as the sum of the cubes of each digit.

Then, we enter a while loop that continues until “number” becomes 0.

Inside the loop, we extract the last digit of “number” using the modulus operator (%) and store it in the “temp” variable.

We calculate the cube of “temp” and add it to “total”.

We divide “number” by 10 to remove the last digit.

Steps 3-5 are repeated until “number” becomes 0.

Finally, we check if “total” is equal to the original number “num”.

  • If they are equal, we print “num” is an Armstrong number”.
  • Otherwise, we print “num” is not an Armstrong number”.

Leave a Comment

%d bloggers like this: