Armstrong Number in Java

Hi Hello, What I am going to do in this article I will see in detail about Armstrong Number in Java. This article is a favorite of everyone and many people are suffering without knowing what the reason is. This article will be very useful for them.

An Armstrong number is a positive m-digit number that is equal to the sum of the mth powers of their digits. It is also known as pluperfect, or Plus Perfect, or Narcissistic number. It is an OEIS sequence A005188.

For example this is a 3 digit Armstrong number

370 = 33 + 73 + o3
         = 27 + 343 + 0
         = 370
Armstrong Number in Java

Armstrong Number in Java

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.");
    }
}

Output

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

153 is an Armstrong number.

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.");
    }
}

Output

The output will display below for your reference.

8208 is an Armstrong number.

Read Also:

Conclusion

By knowing clearly about Armstrong Number in Java through this article. If you have any doubts please leave a comment via the comment box. Thank You.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment