Write a java program to check Armstrong number. In this program, we are going to write a java code which checks whether an input number is Armstrong or not.

Let’s first understand what is Armstrong number?

## What is Armstrong number?

An Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits.

For example – 153, 371 is an Armstrong number.

**Example 1**: 153 has 3 digits.

153 : 1^{3} + 5^{3} + 3^{3} = 153

**Example 2** – Take an example of 6 which has 1 digit.

6: 6^{1} = 6 It’s an Armstrong number

**Example 3** – Take another example let’s say 15, it has 2 digits.

15 : 1^{2} + 5^{2} = 26

So 15 is not an Armstrong number.

**Example 4** – This time let’s take bigger number 1634, it has 4 digits.

1634 : 1^{4} + 6^{4} + 3^{4} + 4^{4} = 1634

It’s an Armstrong number.

## Java Program to Check Armstrong Number

We have discussed, What is an Armstrong number? Let’s write a java code which takes an input number and checks whether an input number is Armstrong or not.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | //Armstrong Number public class ArmstrongNumber { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println(" Enter a number \n"); int num = in.nextInt(); int temp, sum = 0, mod = 0, len = 0; //Initialize num value to temp variable temp = num; //count no of digits while(temp > 0) { len++; temp = temp / 10; } temp = num; while(temp > 0) { mod = temp % 10; sum = (int) (sum + Math.pow(mod, len)); temp = temp/10; } if(sum == num) { System.out.println("Armstrong number"); } else { System.out.println(" Not a armstrong number "); } } } |

** More Programming Questions **

Java program to find first non-repeated character of a string