## Java Armstrong Number Program with Explanation

Armstrong number: An n-digit number that is the sum of the n-th powers of the digits is called an Armstrong number. Armstrong number is also known as Narcissistic number in Recreational number Theory and also Pluperfect digital invariant (PPDI).

Example of Armstrong Number: 153, 370, 371, 407.

### Explanation:

1)    153  =  (1*1*1) + (5*5*5) + (3*3*3).
153  = (1) + (125) + (27).
153 = 153.
2)    370 = (3*3*3) + (7*7*7) + (0*0*0).
370 = (27) + (343) + (0).

370 = 370.
3)    371 = (3*3*3) + (7*7*7) + (1*1*1).
371 = (27) + (343) + (1).
371 = 371.
4)    407 = (4*4*4) + (0*0*0) + (7*7*7).
407 = (64) + (0) + (343).
407 = 407.

### Java Armstrong number program:

To understand this example, you should have knowledge of the following topics:

Java Armstrong number using while loop:
class Armstrong1
{
public static void main(String[] args)
{
int temp, x = 0,y,n;
n = 153;
temp = n;
while ( temp != 0)
{
y = temp % 10;
x = x + y * y * y;
temp = temp / 10;
}
if( x == n)
System.out.println(n + " is Amstrong Number ");
else
System.out.println(n + " is Not a Amstrong Number");
}

}
Output:
153 is Armstrong Number.

### Java Armstrong number program using Recursion:

Java Armstrong number using fo loop:
class Armstrong2
{
int x;
int checkArmstrong(int i,int j)
{
if(i!=0)
{
x=i%10;
j=j+(x*x*x);
i=i/10 ;
return checkArmstrong(i,j);
}
return j;
}
public static void main(String[] arg)
{
Armstrong2 a1 = new Armstrong2();
int a;
System.out.println("Armstrong numbers between 1 to 1000");
for(int n=1;n<1000;n++)
{
a =a1.checkArmstrong(n,0);
if(a == n)
System.out.println(n);
}
}
}

Output:
Armstrong numbers between 1 to 1000
1
153
370
371
407

#### Java Armstrong number program using Buffer reader:

Java Armstrong number using if else:
import java.io.*;
class Armstrong3
{
public static void main(String[] arg) throws IOException
{
int a,j=0,n,temp;
System.out.println("Enter a number");
temp=n;
while(n!=0)
{
a=n%10;
j=j+(a*a*a);
n=n/10;
}
if(j == temp)
System.out.println(temp + " is a armstrong number ");
else
System.out.println(temp + " is not a armstrong number ");
}

}

Output:

Enter a number
115
115 is not a armstrong number

• ()