Let’s learn java program to check armstrong number.

## Java program to check armstrong number

**What is armstrong number in java?**

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 given number is stored in an integer variable. In the next step this value is stored temporary variable.

Also read – palindrome number in java

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

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

In the next step temporary variable last digit is removed and divided by 10. Finally given number and output is compared.

Also read – armstrong number program in java using scanner

If it is equal then it is an armstrong number else it’s not. Here let’s check whether a given number is armstrong number or not using while loop,

import java.util.*; 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.

**Armstrong number in java for n digits**

Similarly we can check whether a given number is armstrong number or not for n digits,

Also read – java program for armstrong number between 1 to 1000

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.

Also read – 4 digit armstrong number in java

Also read – armstrong number in java using recursion