Let’s learn armstrong number in java and java program to check armstrong number.

#### Java program to check armstrong number

**What is armstrong number?**

Armstrong number is a positive integer and is sum of cube of its own digits equal to number itself.

In the armstrong program given number is stored in an integer variable. In the next step this value is stored temporary variable.

Next in while loop given number is looped until equal to zero. Then on each iteration temporary variable last digit is stored in remainder.

Also read – Armstrong numbers between 1 to 1000

Using math.pow() function three number digits in remainder is stored in output variable.

Then temporary variable last digit is removed and divided by 10. Finally given number and output is compared. If it is equal it is an armstrong number else it’s not.

Here let’s check armstrong number in java using while loop,

public class ArmstrongNumberDemo

{

public static void main(String[] args)

{

int num = 371, temp, remainder, output = 0;

temp = num;

while(temp != 0)

{

remainder = temp % 10;

output += Math.pow(remainder, 3);

temp /= 10;

}

if(output == num)

{

System.out.println(num + " is an armstrong number.");

}

else

{

System.out.println(num + " is not an armstrong number.");

}

}

}

**Output:**

371 is an armstrong number.

In the same way above armstrong number java can be executed for n digits,

public class ArmstrongForLoop

{

public static void main(String[] args)

{

int num = 1944, input, remainder, output = 0, a = 0;

input = num;

for(;input != 0; input /= 10, ++a);

input = num;

for(;input != 0; input /= 10)

{

remainder = input % 10;

output += Math.pow(remainder, a);

}

if(output == num)

{

System.out.println(num + " is an armstrong number.");

}

else

{

System.out.println(num + " is not an armstrong number.");

}

}

}

**Output:**

1944 is not an armstrong number.