# Java Program To Check Armstrong Number

In this tutorial, we will learn how to check whether the number is Armstrong or not.

A number of n digits are said to be an Armstrong number if the sum of each digit raised to the power n is equal to the number itself. for example, a 3 digit number is said to be an Armstrong number if the sum of the cubes of the digit is equal to the number itself.

But before moving forward if you are not familiar with the concept of loops in java, then do check the article on Loops in Java

Input: Enter the number: 153

Output: Yes the entered number 153 is an Armstrong number.

## Java Program to Check Armstrong Number

In this program, we will see how to find whether a number is Armstrong or not.

### Algorithm:

1. Start

2. Create an instance of the Scanner class.

3. Declare a variable.

4. Ask the user to initialize the number.

5. Define 3 methods, one to check Armstrong, another to calculate the order, and another to calculate the raised to the power value.

6. Calculate the total number of digits in order function using a while loop.

7. Calculate the value of digit raised to the power order value.

8. In the isArmstrong method check whether the entered number is Armstrong or not.

9. If found Armstrong, then return true else return false.

10. If true, then print a message that the entered number is an Armstrong.

11. If false, then print a message that the entered number is not an Armstrong.

12. Display the message.

13. Stop.

Below is the Java code to check Armstrong number.

``````//Java Program to check Armstrong Number
import java.util.*;
public class checkArmstrong
{
int pow(int x, long y)
{
if( y == 0)
return 1;
if (y%2 == 0)
return pow(x, y/2)*pow(x, y/2);
return x*pow(x, y/2)*pow(x, y/2);
}
int order(int num)
{
int n = 0;
while (num != 0)
{
n++;
num = num/10;
}
return n;
}
boolean isArmstrong (int num)
{
// Calling order function
int n = order(num);
int temp=num, sum=0;
while (temp!=0)
{
int r = temp%10;
sum = sum + pow(r,n);
temp = temp/10;
}
// If satisfies Armstrong condition
return (sum == num);
}
// Driver Program
public static void main(String[] args)
{
//Take input from the user
//Create an instance of the Scanner class
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number ");
int num = sc.nextInt();
checkArmstrong obj = new checkArmstrong();

if(obj.isArmstrong(num))
{
System.out.println("Yes "+num+" is an Armstrong number");
}
else
{
System.out.println(num+" is not an Armstrong number");
}
}
} ``````

Enter the number 1634
Yes 1634 is an Armstrong number

## Program 2: Check Armstrong Number in Java

In this program, we will see how to find the Armstrong numbers between 0 to 1000.

### Algorithm:

1. Start

2. Create an instance of the Scanner class.

3. Declare two variables for the range.

4. Ask the user to initialize the range.

5. Declare three other variables to store the original number, sum, and digit.

6. Use a for loop to iterate through all the elements within that range.

7. Initialize the temp variable to the loop variable.

8. Initialize the sum to 0.

9. Find the sum of cubes of each digit.

10. Check if the sum is equal to the original number.

11. If equal, then the number is Armstrong.

12. If not equal, then the number is not Armstrong.

13. Display all the Armstrong numbers.

14. Stop

Below is the Java code to check Armstrong number.

``````//Java Program to print all the Armstrong Number from 0 to 1000
import java.util.*;
public class Main
{
public static void main(String []args)
{
//Take input from the user
//Create an instance of the Scanner class
Scanner sc=new Scanner(System.in);
System.out.println("Enter the starting range: ");
int num1=sc.nextInt();
System.out.println("Enter the ending range: ");
int num2=sc.nextInt();
int temp, digit, sum;

for (int i = num1; i < num2; i++) {
temp = i;
sum = 0;
while (temp != 0)
{
digit = temp % 10;
sum = sum + digit * digit * digit;
temp /= 10;

}
if (sum == i)
System.out.println(i + " is an Armstrong Number");
}
}
}``````

Enter the starting range: 0
Enter the ending range: 1000
0 is an Armstrong Number
1 is an Armstrong Number
153 is an Armstrong Number
370 is an Armstrong Number
371 is an Armstrong Number
407 is an Armstrong Number