Java Program to check whether a number is Armstrong or not

December 13, 2022, Learn eTutorial

Here we are explaining how to write a Java program to check whether a 3-digit number of base 10 is Armstrong or not. It is a simple number based program implemented with mathematical operations and loops in Java.

To understand more about the concepts on Armstrong Numbers, we assure you our blog The basics of Armstrong numbers will help you very much

We recommend you the following topics of Java for a better understanding of the program.

How to check whether a 3-digit number of base 10 is Armstrong or not?

A 3-digit number is said to be an Armstrong number, if 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 digit are shown below
    • 33=27
    • 73=343
    • 13=1
  • Then the sum of the cubes of the digits = 27+343+1 = 371


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

First and foremost step is to declare a java class named ArmstrongNum.Then declare the required variables. Create an object of the scanner class as sc and read the number from the user into the variable num. Assign originalNum=num. Find the sum of cubes of digits of num by using a while loop with condition num != 0,

  • Take each digit by finding the remainder of num divided by 10, digit=num
  • Find the cube of digit using pow() method of Java, add it with cubeSum
  • To find the next digit assign num = num/10.

After exiting from the loop, check if cubeSum is the same as originalNum, if yes then display the number is Armstrong number, else display the number is not Armstrong number using system.out.println().


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, digit, cubeSum, num

STEP 4: Assign cubeSum = 0

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

STEP 6: Read the number into the variable num

STEP 7: Assign originalNum=num

STEP 8: By using a while loop with the condition originalNum !=0, do steps 9,10

STEP 9: Find the digit, cube of that digit, and the sum of cubes into cubeSum

STEP 10: Then to find the next digit divide the num by 10

STEP 11: After processing loop, check if cubeSum is equal to originalNum

  • if true then display the number is Armstrong number
  • else display the number is not Armstrong.

Java Source Code

                                          import java.util.Scanner;

public class ArmstrongNum {

    public static void main(String[] args) {

        int originalNum, digit, cubeSum = 0,num;
        System.out.println("Enter the number:");
        Scanner sc = new Scanner(System. in );
        num = sc.nextInt();
        originalNum = num;

        while (num!= 0)
            digit = num % 10;
            cubeSum += Math.pow(digit, 3);
            num /= 10;

        if(cubeSum == originalNum)
            System.out.println(originalNum+ " is an Armstrong number");
            System.out.println(originalNum+ " is not an Armstrong number");


Enter the number: 371
371 is an Armstrong number