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.
Input : 153
Process : (1*1*1)+(5*5*5)+(3*3*3) = 1+125+27 = 153
Output : It is Armstrong Number
Input : 370
Process : (3*3*3)+(7*7*7)+(0*0*0) = 27+343+0 = 370
Output : It is Armstrong Number
Input : 132
Process : (1*1*1)+(3*3*3)+(2*2*2) = 1+27+8 = 36
Output : It is not an Armstrong Number
Program :-
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
Note : The program is written in the easiest way possible so try understand it. If you face any problem in understanding this program than feel free to contact/comment.
All the best :)
Keep Learning :)
Watch this Program Video : https://youtu.be/ThIt0vCoQTc
Comments