Let’s learn java program to check armstrong number.

## Java program to check armstrong number

In this post let us learn to check whether given number is armstrong number or not.

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

In java program for armstrong number first given number (371) is stored in an integer variable ‘num’.

In the next step this value is stored in another integer variable ‘a’.

Next in while loop integer variable ‘a’ is looped until it is equal to zero.

Then on each iteration last digit of variable ‘num’ is stored in remainder.

Then using math.pow() function remainder is powered by 3 and added to variable ‘output’.

In the next step last digit is removed from variable ‘a’ after dividing it by 10.

Finally variable ‘num’ and ‘output’ are compared.

If it is equal then it is an armstrong number else it’s not.

Let’s check whether a given number is an armstrong number.

import java.util.*; public class ArmstrongNumberInJava { public static void main(String[] args) { int num = 371, a, remainder, output = 0; a = num; while(a != 0) { remainder = a % 10; output += Math.pow(remainder, 3); a /= 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.

Also read – methods in java