Java Program to Check Armstrong Number or Not

This article covers multiple programs in Java that checks whether a number entered by user at run-time of the program, is an Armstrong number or not.

An Armstrong number is a number that is equal to the sum of its own digits, where each digit raised to the power of number of digit. For example, 153 is an Armstrong number, because:

153 = 13 + 53 + 33
    = 1 + 125 + 27
    = 153

Some other Armstrong numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 370, 371, 407, 1634 etc.

Check Armstrong Number in Java using while Loop

The question is, write a Java program to check whether a number is an Armstrong number or not. The number must be received by user at run-time. The program given below is its answer:

import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      int num, temp, totalDigit=0, res=0, rem, pow, i;
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter the Number: ");
      num = scan.nextInt();
      
      temp = num;
      while(num>0)
      {
         num = num/10;
         totalDigit++;
      }
      
      num = temp;
      while(num>0)
      {
         rem = num%10;
         pow = 1;
         i = 0;
         while(i<totalDigit)
         {
            pow = pow*rem;
            i++;
         }
         res = res + pow;
         num = num/10;
      }
      
      if(res==temp)
         System.out.println("\nArmstrong Number");
      else
         System.out.println("\nNot an Armstrong Number.");
   }
}

The snapshot given below shows the sample run of above program with user input 407 as number to check whether it is an Armstrong number or not:

java program check armstrong number

The above program can also be written as. This program uses Math.pow() method to directly calculate the value, of a number to the power of another number:

import java.util.Scanner;
import java.lang.Math;

public class CodesCracker
{
   public static void main(String[] args)
   {
      int num, temp, totalDigit=0, res=0, rem;
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter the Number: ");
      num = scan.nextInt();
      
      temp = num;
      while(num>0)
      {
         num /= 10;
         totalDigit++;
      }
      
      num = temp;
      while(num>0)
      {
         rem = num%10;
         res = res + (int) Math.pow(rem, totalDigit);
         num /= 10;
      }
      
      if(res==temp)
         System.out.println("\nArmstrong Number");
      else
         System.out.println("\nNot an Armstrong Number.");
   }
}

The sample run of above program with user input 371 is shown in the snapshot given below:

check armstrong number or not in java

Check Armstrong Number in Java using for Loop

The previous program, that was created using while loop, can also be created using for loop, as shown here:

import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      int num, temp, totalDigit=0, res=0, rem;
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter the Number: ");
      num = scan.nextInt();
      
      for(temp=num; num>0; num /= 10)
         totalDigit++;
      
      for(num=temp; num>0; num /= 10)
      {
         rem = num%10;
         res = res + (int) Math.pow(rem, totalDigit);
      }
      
      if(res==temp)
         System.out.println("\nArmstrong Number");
      else
         System.out.println("\nNot an Armstrong Number.");
   }
}

Check Armstrong Number in Java - Only for 3-Digit Number

Here is another way, that we can create the same program. But this program is limited to 3-digit number only:

import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      int num, temp, res=0, rem;
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter the Number: ");
      num = scan.nextInt();
      
      temp = num;
      while(temp!=0)
      {
         rem = temp%10;
         res = res + (rem*rem*rem);
         temp = temp/10;
      }
      
      if(num==res)
         System.out.println("\nArmstrong Number.");
      else
         System.out.println("\nNot an Armstrong Number.");
   }
}

Same Program in Other Languages

Java Online Test


« Previous Program Next Program »


Liked this post? Share it!