An  Armstrong number is a number that is equal to the sum of the digits in a number raised to the power of a number of digits in the number. For example: If we take 371, it is an Armstrong number as the number of digits here is 3, so

371 = 33 + 73 + 13 = 27 + 343 + 1 = 371

Similarly, 153 is Armstrong because 13 + 53 + 33 equals to 153. Another Example is 9474, here the number of digits is 4, so

9474 = 94 + 44 + 74 + 44 = 6561 + 256 + 2401 + 256 = 9474.

And yes, 0 and 1 are also Armstrong number.

### Java program for armstrong number

Now, as we know the concept of armstrong number, Let's create the Java program for Armstrong number using while loop

``````package com.armstrong;

import java.util.Scanner;

public class ArmstrongNumberCheck {
public static void main(String args[])
{
int number, sum = 0;

Scanner in = new Scanner(System.in);
System.out.println("Enter a number to check if it is an armstrong number");
number = in.nextInt();

/* Converting Integer to String. It'll help to find number of
digits in the Integer by using length() */
String temp = number + "";
//get number length
int numLength = temp.length();
//copy the main Number in a variable
int numCopy = number;

//loop all the numbers until remainder is 0
while(numCopy != 0 ){
int remainder = numCopy % 10;
// using Math.pow to get digit raised to the power
// total number of digits
sum = sum + (int)Math.pow(remainder, numLength);
numCopy = numCopy/10;
}

System.out.println("sum is " + sum );

if (number == sum )
{
System.out.println( number + " is an armstrong number.");
}
else
{
System.out.println(number+ " is not an armstrong number.");
}
in.close();

}
}
``````

Output:

``````Enter a number to check if it is an armstrong number
9474
sum is 9474
9474 is an armstrong number.

Enter a number to check if it is an armstrong number
121
sum is 10
121 is not an armstrong number.

Enter a number to check if it is an armstrong number
371
sum is 371
371 is an armstrong number.
`````` ### Program to generate Armstrong number up too N values in Java

``````package com.armstrong;

import java.util.Scanner;

public class ArmstrongNumberCheck {
public static void main(String args[])
{
int number, sum = 0,n;

Scanner in = new Scanner(System.in);
System.out.println("Enter a number up to which you want to check armstrong number");
n = in.nextInt();
for(int i=0; i<n; i++)
{

number=i;
/* Converting Integer to String. It'll help to find number of
digits in the Integer by using length() */
String temp = number + "";
//get number length
int numLength = temp.length();
//copy the main Number in a variable
int numCopy = number;

//loop all the numbers until remainder is 0
while(numCopy != 0 ){
int remainder = numCopy % 10;
// using Math.pow to get digit raised to the power
// total number of digits
sum = sum + (int)Math.pow(remainder, numLength);
numCopy = numCopy/10;
}

if (number == sum )
{
System.out.println( number + " is an armstrong number.");
}
//set to sum=0 again for next value
sum=0;
}
in.close();

}
}
``````

Output:

``````Enter a number up to which you want to check armstrong number
500
0 is an armstrong number.
1 is an armstrong number.
2 is an armstrong number.
3 is an armstrong number.
4 is an armstrong number.
5 is an armstrong number.
6 is an armstrong number.
7 is an armstrong number.
8 is an armstrong number.
9 is an armstrong number.
153 is an armstrong number.
370 is an armstrong number.
371 is an armstrong number.
407 is an armstrong number.
``````

You may also like:

How to compare Strings in Java?

Sorted Single Linked List in java

Fibonacci series program in Java (With and without recursion)