Armstrong Number in Java

You are currently viewing Armstrong Number in Java

Hello reader, If you want to know whether the given number is an Armstrong number or not then read this article to learn about it. Yes, I’m going to teach the topic of the “Armstrong Number in Java” program here. Let’s start the article.

What is an Armstrong Number?

A number that is equal to the sum of cubes of its digits is called an Armstrong Number also known as a Narcissistic Number.

For example, If we can choose the input number as “407” then the output will be like ( 4*4*4+0*0*0+7*7*7=64+0+343=407 ).

Armstrong Number in Java

Here, We can find the Armstrong number in java for 3 digits.

public class Armstrong {

    public static void main(String[] args) {

        int number = 153, 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.");
    }
}

Explanation

Initially, We need to declare the respective variable which will use to store the input data. Secondly, The three-digit input number will iterate using the while loop.

Thirdly, The given input number is divisible by 10 and then it will store in the remainder variable. Later, The Math.pow() function will use to find the cube of each digit.

When the remainder of the input number is divisible by 10, it is the last digit of the input integer. Here, The If…else condition will help to check whether the given number is an Armstrong number or not.

If the result is equal to the given number then it will print “it is an Armstrong number” using the if…condition. Otherwise, It will print “it is not an Armstrong number” using the else condition.

The output will display on the screen using the system.out.printIn() function.

Output

The output of Armstrong number in java will show below for your reference.

153 is an Armstrong number.
Read Also : Find Area Of Triangle in Java

Note

You can also find Armstrong Number in C, Armstrong Number in C++. If you are looking to know then please read those article which we have already published on our webpage.

Armstrong Number in Java

Now, We can find the Armstrong number in java for n digits.

public class Armstrong {

    public static void main(String[] args) {

        int number = 8208, 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.");
    }
}

Execution

In this program, We can count the number of digits in the number using the first for loop whereas the remainder will be raised by the number of digits when we calculate the output on each iteration. Otherwise, The execution of both programs will be the same.

Output

The output will display below for your reference.

8208 is an Armstrong number.

Conclusion

I hope this article helps you to check whether a number is an Armstrong Number or not in Java Program. If you face any issues please let me know via the comment section. Share this feature with other Java program developers via social networks.

Manoj S

Hi, I'm Manoj working as a part-time blogger in Coding Diksha. Here, I post about programming to help developers.

Leave a Reply