Armstrong Number in Java Program

Armstrong Number in Java

 

Armstrong Numbers

Armstrong number is a mathematical phenomenon. It is an N-digit number who is equal to the sum of nth power of itself. In java that is very frequently asked question to find Armstrong numbers or compare Armstrong numbers.

Armstrong Number in Java Program

Now we are going to explain how to mathematically find Armstrong Number in Java Program.

  • Find the digit value of the numbers
  • Give the power equal to the digit value to individual element.
  • Sum up the elements

For Explanation

Lets take 124 number for finding that is it a Armstrong number or not?

124 has three digit value so 3 is the power of all element

13+23+43=73

So the sum of the value is 73 that is not equal to original value so it is not Armstrong number.

Lets take another example that is 153 that’s again a 3 digit value

13+53+33=153

The sum is 153 that is equal to the original value so 153 is a Armstrong number.

Armstrong Number in Java Program Code

Explanation of the code

Now lets start understanding the how this code works.

In this firstly save the original value to the temp value because of the further use and one of the reasons that during the execution original value may be change, we must have the original copy for compare in the future.

Start the while loop until 153 is equal to 0

The first line says find modulo of the original value that is 153 and must save in temp value

r=153%10

= 3

Now a has the value equal to 3 and

Now the second line says

Value=value+a*a*a;

Value initially define as zero

Value=0+3*3*3

Value=27;

And third line says

Temp=temp/10;

Temp=153/10

Temp=15;

Now temp has been change to 15

Again temp value is not equal to zero so the condition of the while loop is true then again process starts

r=15%10

r=5;

value=27+5*5*5

value=152;

temp=15/10

temp=1

now again temp has been change to the value 1

so temp is not equal to zero again while starts.

r=1%10

r=1;

value=152+1*1*1

value=153;

temp=1/10

temp=0

Now value of temp variable become zero so while loop exits.