# Java Program to Check Armstrong Number

JavaObject Oriented ProgrammingProgramming

In this article, we will understand how to check if the given number is an Armstrong number. An Armstrong number is a number that is equal to the sum of the cubes of its own digits.

An integer is called an Armstrong number of order n if it's every digit separate out and cubed and summed up then the sum will be same as the number i.e. abcd... = a3 + b3 + c3 + d3 + ...

In case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself. For example: 153 is an Armstrong number.

153 = 13 + 53 + 33

For example: 371 is an Armstrong number.

Below is a demonstration of the same −

Input

Suppose our input is −

Enter the number : 407

Output

The desired output would be −

407 is an Armstrong number

## Algorithm

Step 1 - START
Step 2 - Declare four integer values namely my_input, my_temp, my_remainder, my_result
Step 3 - Read the required values from the user/ define the values
Step 4 - Run a while loop to check Armstrong numbers using %, / and * operator
Step 5 - Divide by 10 and get remainder for ‘check’ .
Step 6 - Multiply ‘rem’ thrice, and add to ‘sum’, and make that the current ‘sum’.
Step 7 - Divide ‘check’ by 10, and make that the current ‘check’. Store the resultant value.
Step 8 - If the resultant value is equal to the input value, the input value is an Armstrong number, else it’s not an Armstrong number
Step 9 - Display the result
Step 10- Stop

## Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool .

import java.util.Scanner;
public class IsArmstrong {
public static void main(String[] args) {
int my_input, my_temp, my_remainder, my_result;
my_result = 0;
System.out.println("Required packages have been imported");
Scanner my_scanner = new Scanner(System.in);
System.out.println("A reader object has been defined ");
System.out.print("Enter the number : ");
my_input = my_scanner.nextInt();
my_temp = my_input;
while (my_temp != 0){
my_remainder = my_temp % 10;
my_result += Math.pow(my_remainder, 3);
my_temp /= 10;
}
if(my_result == my_input)
System.out.println(my_input + " is an Armstrong number");
else
System.out.println(my_input + " is not an Armstrong number");
}
}

## Output

Required packages have been imported
A reader object has been defined
Enter the number : 407
407 is an Armstrong number

## Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class IsArmstrong {
public static void main(String[] args) {
int my_input, my_temp, my_remainder, my_result;
my_input = 407;
my_result = 0;
System.out.println("The number is defined as " +my_input);
my_temp = my_input;
while (my_temp != 0){
my_remainder = my_temp % 10;
my_result += Math.pow(my_remainder, 3);
my_temp /= 10;
}
if(my_result == my_input)
System.out.println(my_input + " is an Armstrong number");
else
System.out.println(my_input + " is not an Armstrong number");
}
}

## Output

The number is defined as 407
407 an Armstrong number
Updated on 22-Feb-2022 11:20:53