Hey guys!! Welcome to flower brackets blog. In this article let us learn java program to check armstrong number.

#### Java program to check armstrong number

In the below java program first given number is stored in integer variable. Then this given number value is stored temporary variable.

Next in while loop given number is looped until it’s 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.

Finally temporary variable last digit is removed and divided by 10. Lastly 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 java program 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.