4 digit armstrong number in java

Let’s learn 4 digit armstrong number in java.

4 digit armstrong number in java

Now let’s learn to check armstrong number for 4 digit.

Here’s the program that checks whether the given number is 4 digit armstrong number or not.

public class ArmstrongNumberDemo
{
   public static void main(String[] args) 
   {
      int num = 9474, realNumber, remainder, output = 0, a = 0;
      realNumber = num;
      for(;realNumber != 0; realNumber /= 10, ++a);
      realNumber = num;
      for(;realNumber != 0; realNumber /= 10)
      {
         remainder = realNumber % 10;
         output += Math.pow(remainder, a);
      }
      if(output == num)
      {
         System.out.println(num + " is an Armstrong number.");
      }
      else
      {
         System.out.println(num + " is not an Armstrong number.");
      }
   }
}


Output:

9474 is an Armstrong number.


Also read – do while loop in java