Armstrong Number in JAVA

Armstrong Number


Program to check a number is Armstrong or Not.

A Number is said to be Armstrong Number if it is equal to the sum of cubes of its digits.

Example : 0, 1, 153, 370, 371, 407 etc.

Program below:-

import java.io.*;
import java.util.*;
class Armstrong
{
    public static void main(String args[]) throws IOException
    {
        int n,m,rev,s=0;
        Scanner in= new Scanner(System.in);
        System.out.println("Enter the number");
        n= in.nextInt(); // Enter Number from user
        m=n; // storing entered number in m variable
        while(n>0)
        {
            rev= n%10; // extracting last digit of the number
            s=s+rev*rev*rev; // store cube of the digit
            n=n/10; // extracting quotient of the number
        }
        if(s==m) // checking entered number with cubes of its digits
        {
            System.out.println("It is an armstrong number : "+m);
        }
        else
        {
            System.out.println("It is not an armstrong number : "+m);
        }
    }// end of main method
}// end of class

All the best :)

Watch this Program Video : https://youtu.be/ThIt0vCoQTc

Comments

Popular posts from this blog

Frequency of each digit of a number in Java

Trimorphic Number in JAVA

Tri-Automorphic Number in JAVA