In this tutorial, we will learn how to write a program to check whether a number is an Armstrong number or not in java.
What is Armstrong number?
A number is said to be an armstrong number if the sum of cube of its digit is equal to the number itself. Examples of armstrong numbers are 0, 1, 153, 370, etc.
We can check if a number is armstrong manually, as follows -
153 = 13 + 53 + 33 = 1 + 125 + 27 = 153 (153 is an armstrong)
41 ≠ 43 + 13 = 64 + 1 = 65 (41 is not an armstrong)
1. Take the number to be checked and create a copy of the number numberCopy. This new variable numberCopy will be used to perform operations on the number in the program, so that our original number remains unaltered.
2. Create new variable sumOfCube to store the sum of cube of digits of number.
3. Create an integer variable digit to store the digit one by one.
4. Iterate over the number stored in numberCopy digit by digit, until the number becomes zero.
a. Extract last digit from number
b.Add cube of digit to the sumOfCube; we can also use pow() in java to calculate the cube of digit extracted.
c. Now, divide numberCopy by 10 to reduce one last digit from it.
5. Lastly, when the iteration is over, we will check if the sumOfCube calculated is equal to the original number, number. If yes, then number is an armstrong number else the number is not an armstrong number. Printing the results accordingly.
The following is the program code to check whether a number is an Armstrong number or not in java. In this program, we have a number in number variable to be checked.
public class ArmstrongNumber
public static void main(String args)
int number = 41;
int numberCopy = number;
int sumOfCube = 0;
int digit = 0;
while(numberCopy != 0)
digit = numberCopy % 10;
sumOfCube = sumOfCube + digit * digit * digit;
numberCopy = numberCopy / 10;
if(sumOfCube == number)
System.out.println("Number is an Armstrong number");
System.out.println("Number is not an Armstrong number");
1. When the 'number' has value equal to 153, the program output is shown below:
Number is an Armstrong number
2. When the 'number' has value equal to 41, the program output is shown below:
Number is not an Armstrong number