Armstrong Number Java

Hello everyone!! Welcome to flower brackets blog. So now in this post you will learn armstrong number java.

Armstrong Number Java

Armstrong Number

Basically an armstrong number is sum of cube of its own digits. Let me explain armstrong number in java with an example,

153 is an Armstrong number. How?

Here it is,

153 = (1 * 1 * 1) + (5 * 5 * 5) + (3 * 3 * 3)

Now cubes of above digits is,

(1 * 1 * 1) = 1

(5 * 5 * 5) = 125

(3 * 3 * 3) = 27

So, 1 + 125 + 27 = 153

Also Read – Java Garbage Collection

Hence, 153 is an Armstrong number where sum of the cubes of its digits is equal to number itself. Now let’s see java program to check armstrong number

Example: armstrong number java

import java.util.Scanner;

public class ArmstrongNumber
{
   public static void main(String[] args)
   {
      int x;
      int y;
      int z = 0;
      int temp;

      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a number : ");
      x = sc.nextInt();

      temp = x;
      while(x > 0)
      {
         y = x % 10;
         x = x / 10;
         z = z + (y * y * y);
      }

      if(temp == z)
      {
         System.out.println(temp + " is an Armstrong Number.");
      }
      else
      {
         System.out.println(temp + " is not an Armstrong Number.");
      }
   }
}

Output:

Please enter a number : 153
153 is an Armstrong Number.


Java program for armstrong number between 1 to 1000

Here let’s learn how to print armstrong numbers between 1 to 1000,

public class ArmstrongNumberExample
{
   public static void main(String[] args)
   {
      int number, b, total = 0;
      System.out.print("Armstrong numbers between 1 to 1000 : ");
      for(int a = 1; a <= 1000; a++)
      {
         number = a;
         while(number > 0)
         {
            b = number % 10;
            total = total + (b * b * b);
            number = number / 10;
         }
         if(total == a)
         {
            System.out.print(a + " ");
         }
         total = 0;
      }
   }
}

Output:

Armstrong numbers between 1 to 1000 : 1 153 370 371 407


Armstrong number in java using while loop

Here’s an example java program for armstrong number,

public class ArmstrongNumberDemo
{
   public static void main(String[] args)
   {
      int num = 153, temp, remainder, output = 0;

      temp = num;

      while(temp != 0)
      {
         remainder = temp % 10;
         output += Math.pow(remainder, 3);
         temp /= 10;
      }

      if(output == num)
      {
         System.out.println(num + " is an armstrong number.");
      }
      else
      {
         System.out.println(num + " is not an armstrong number.");
      }
   }
}

Output:

153 is an armstrong number.


Armstrong number in java using function

In this java program we will check using function,

public class ArmstrongNumberFunction
{
   int power(int a, long b)
   {
      if( b == 0)
         return 1;
      if(b % 2 == 0)
         return power(a, b / 2) * power(a, b / 2);
      return a * power(a, b / 2) * power(a, b / 2);
   }
 
   // calculating order of the number
   int order(int a)
   {
      int num = 0;
      while(a != 0)
      {
         num++;
         a = a / 10;
      }
      return num;
   }
 
   // this function checks whether the given number is armstrong number or not
   boolean checkArmstrong(int a)
   { 
      int num = order(a);
      int temp = a, sum = 0;
      while(temp != 0)
      {
         int r = temp % 10;
         sum = sum + power(r, num);
         temp = temp / 10;
      }
      // if Armstrong condition is satisfied
      return(sum == a);
   } 
 
   public static void main(String[] args)
   {
      ArmstrongNumberFunction anf = new ArmstrongNumberFunction();
      int a = 153;
      System.out.println(anf.checkArmstrong(a));
      a = 1253;
      System.out.println(anf.checkArmstrong(a));
   }
}

Output:

true
false


Conclusion

That’s it guys. This is all about java program to check armstrong number.

I hope you guys have understood the concept. You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

Related Posts