Armstrong Number Java

Hello everyone!! Welcome to flower brackets blog. So now in this post you will learn 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,

Armstrong number example:

Armstrong Number Java

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.

example: Armstrong Number Java

Let’s see an example on java program for armstrong number,

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 to find if it is an Armstrong 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 to find if it is an Armstrong number: 153
153 is an Armstrong Number.


using for loop

Now let’s learn armstrong number in java using for loop,

import java.util.Scanner;

public class ArmstrongNumber
{
   public static void main(String[] args)
   {
      int a, number, temp, result = 0;
      System.out.println("Please enter a number to find if it is an armstrong number : ");
      Scanner sc = new Scanner(System.in);
      a = sc.nextInt(); 
      number = a;

      for( ; number != 0; number /= 10)
      {
         temp = number % 10;
         result = result + temp * temp * temp;
      }

      if(result == a)
         System.out.println(a + " is an Armstrong number");
      else
         System.out.println(a + " is not an Armstrong number");
      sc.close();
   }
}

Output:

Please enter a number to find if it is an armstrong number : 371
371 is an Armstrong number


armstrong numbers between 1 to 1000

Here let’s see armstrong number program in java 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


using while loop

Here’s an example java program for armstrong number,

import java.util.Scanner;

public class ArmstrongNumberDemo
{
   public static void main(String[] args)
   {
      int x, y, z, total = 0, number, count = 0;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter a number to find if it is an armstrong number : ");
      number = sc.nextInt();
      x = number;
      z = number;
      while(x > 0)
      {
         x = x / 10;
         count++;
      }
      while(number > 0)
      {
         y = number % 10;
         total = (int) (total + Math.pow(y, count));
         number = number / 10;
      }
      if(total == z)
      {
         System.out.println("It is an armstrong number.");
      }
      else
      {
         System.out.println("Not an armstrong number.");
      }
      sc.close();
   }
}

Output:

Please enter a number to find if it is an armstrong number : 446
Not an armstrong number.


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


using given values

Let’s see how to write a program for armstrong number in java using given values,

public class ArmstrongJava
{
   public boolean checkArmstrongNumber(int number)
   { 
      int a = number;
      int noOfDigits = String.valueOf(number).length();
      int sum = 0;
      int div = 0; 
      while(a > 0) 
      { 
         div = a % 10; 
         int temp = 1;
         for(int x = 0; x < noOfDigits; x++)
         {
            temp *= div;
         }
         sum += temp;
         a = a / 10; 
      } 
      if(number == sum) 
      {
         return true; 
      }
      else
      {
         return false; 
      } 
   }

   public static void main(String[] args)
   {
      ArmstrongJava arm = new ArmstrongJava();
      System.out.println("Is 371 Armstrong number ? " + arm.checkArmstrongNumber(371));
      System.out.println("Is 523 Armstrong number ? " + arm.checkArmstrongNumber(523));
      System.out.println("Is 153 Armstrong number ? " + arm.checkArmstrongNumber(153));
   }
}

Output:

Is 371 Armstrong number ? true
Is 523 Armstrong number ? false
Is 153 Armstrong number ? true


conclusion

That’s it guys. This is all about what is armstrong number and armstrong number program in java.

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.

You May Also Like