Check if the given number is armstrong number

Q. Write a program to check if the given number is Armstrong number.

Answer:

An Armstrong number is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 = 3*3*3 + 7*7*7 + 1*1*1

Armstrong.java

class Armstrong
{
        public static void main(String[] args)
        {
                int n = 371;
                int rem;
                int n1 = 0;
                int temp;
                temp = n;
                while (n>0)
                {
                        rem = n % 10;
                        n = n / 10;
                        n1 = n1 +(rem * rem * rem);
                }
                if (temp == n1)
                {
                        System.out.println("The "+temp+" is armstrong number");
                }
                else
                        System.out.println("The "+temp+" is not armstrong number");
        }
}


Output:

armstrong number