What is Armstrong Number ?
In an Armstrong Number, the sum of power of individual digits is equal to number itself.
In other words the following equation will hold true
xy..z = xn + yn+.....+ zn
n is number of digits in number
For example this is a 3 digit Armstrong number
370 = 33 + 73 + 03 = 27 + 343 + 0 = 370
Examples of Armstrong Numbers
0, 1, 4, 5, 9, 153, 371, 407, 8208, etc.
Program:
import java.util.*;
class Armstrong
{
public static void main(String args[])
{
int n,s=0,r,t;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number to check its an armstrong or not:");
n=sc.nextInt();
t=n;
while(n!=0)
{
r=n%10;
s=s+(r*r*r);
n=n/10;
}
if(t==s)
{
System.out.println("Entered number is an armstrong");
}
else
System.out.println("Entered number is not an armstrong");
}
}
Output:
Enter the number to check its an armstrong or not:
123
Entered number is not an armstrong
Enter the number to check its an armstrong or not:
153
Entered number is an armstrong