 # Java Program to check whether a number is amstrong or not

Here we are explaining how to write a java program to check whether a number is Amstrong or not.

## How to check whether a number is Amstrong or not?

An Armstrong number is a number, where the sum of the cubes of each digit of the number equals that number.

Example:371

The digits of 371 are 3,7,1. The cubes of each number are shown below.

3*3*3=27

7*7*7=343

1*1*1=1

Then the sum of the cubes of the digit = 27+343+1

= 371

## How to implement the java program to check whether a number is Amstrong or not?

First, we have to declare the class AmstrongNum.Then declare the variables a,b,temp,n as integer.Create an object of the scanner class as sc and read the number from the user into the variable n. Assign temp=n. By using while loop assigns a=n. Now a contains the last digit of n. Assign n=n/10..Then find out the sum of the cubes of all digits as b=b+(a*a*a). After exiting from the loop, check if temp=b, if true then display the number b is amstrong number. Else display the number is not amstrong number using `system.out.println()`.

## ALGORITHM

STEP 1: Declare the class ArmstrongNum with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Declare the integer variables originalNum ,rem ,rs ,n.

STEP 4: Assign rs=0.

STEP 5: Create an object of the scanner class as sc.

STEP 6: Read the number into the variable n.

STEP 7: Assign originalNum=n.

STEP 8: By using a while loop check originalNum>0, if true then do step 9.

STEP 9: Find Math.pow(rem, 3)

STEP 10: Check if rs== n, if true then display the number n is amstrong number.

STEP 11: Else display the number n is not amstrong.

## Java Source Code

```                                          ```import java.util.Scanner;

public class ArmstrongNum {

public static void main(String[] args) {

int originalNum, rem, rs = 0,n;

System.out.println("Enter the number:");

Scanner sc = new Scanner(System. in );

n = sc.nextInt();

originalNum = n;

while (originalNum!= 0)
{
rem= originalNum % 10;
rs += Math.pow(rem, 3);
originalNum /= 10;
}

if(rs== n)
System.out.println(n+ " is an Armstrong number.");
else
System.out.println(n+ " is not an Armstrong number.");
}
}```
```

## OUTPUT

```Enter the number: 371
371 is an Armstrong number```
VIEW ALL
VIEW ALL
##### OtherTutorials
VIEW ALL 