Java Program to Check Armstrong Number

In this program, we will learn How to Check Armstrong Number in Java Programming Language. Let’s understand Armstrong numbers and then we will code the Java Program to Check Armstrong Number.

What is Armstrong Number?

Armstrong numbers are numbers that are equal to the sum of the cubes of its digit. Armstrong numbers are also called Narcissistic Numbers.

For example, 0, 1, 153, 370, 371, and 407 are some Armstrong numbers.

let's see 407,

4³ + 0³ + 7³ = 64 + 0 + 343 = 407

Hence, 407 is an Armstrong number.

Let’s see the Java Program to Check Armstrong Number.

Java Program to Check Armstrong Number

import java.util.*;

public class Main{
    
    public static void main(String[] args){
        
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number : ");
        int number = sc.nextInt();
        
        if(number == 0 || number == 1){
            System.out.println(""+number+" is an Armstrong Number !");
            return;
        }
        
        int num = number; // preserving the original number
        
        int sum = 0;
        
        while(num > 0){
            int digit = num % 10;
            sum += Math.pow(digit,3);
            num = num / 10;
        }
        if(sum == number){
            System.out.println(""+number+" is an Armstrong Number !");
        }else{
            System.out.println(""+number+" is NOT an Armstrong Number !");
        }

    }
    
}

Output 1

Enter the number: 370
370 is an Armstrong Number !

Output 2

Enter the number: 147
147 is NOT an Armstrong Number !

How Does This Program Work ?

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number : ");
        int number = sc.nextInt();

In this Program, first, we take the input of the number from the user using the Scanner Class in Java.

        if(number == 0 || number == 1){
            System.out.println(""+number+" is an Armstrong Number !");
            return;
        }

Then, we check whether the given number is 0 or 1. If the given number is 0 or 1, we display that the given number is an Armstrong number and we return, as there is no need to perform any further calculation to check so.

        int num = number; // preserving the original number
        
        int sum = 0;

then, we declare two variables of int datatype. Variable num stores the value of the number to perform the calculation on digits and by this, the original number is also preserved in variable named number. Then, we assign the variable sum with the value 0 to store the sum of the cube of the digits of the given number.

        while(num > 0){
            int digit = num % 10;
            sum += Math.pow(digit,3);
            num = num / 10;
        }
        if(sum == number){
            System.out.println(""+number+" is an Armstrong Number !");
        }else{
            System.out.println(""+number+" is NOT an Armstrong Number !");
        }

Then, we used a while to get each digit of the number and then we sum the cube of the digits. We used Math.pow() function to cube the digits.

Let’s take an example of number 407,
In first iteration, 407 > 0 so we go into the loop, digit = 407 % 10 = 7, then sum = sum + Math.pow(7,3) = 0 + 7³ = 343.
Then, the num becomes 407 / 10 = 40.

In second iteration, 40 > 0, digit = 40 % 10 = 0, then sum = sum + 0³ = 343 + 0 = 343, and 40 / 10 = 4

In third iteration, 4 > 0, digit = 4 % 10 = 4, then sum = sum + 4³ = 343 + 64 = 407, and 4 / 10 = 0.

Now, as the num is not greater than 0, the while loop is terminated.

Then, we check if the sum is equal to the original number stored in variable number, we print that the given number is an Armstrong number, otherwise, we print the given number is not an Armstrong number.

This is the Java Program to Check Armstrong Number.

Conclusion

I hope after going through this post, you understand Java Program to Check Armstrong number.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *