Armstrong number in java

Let’s learn armstrong number in java.

Armstrong number in java

Check whether a given number is armstrong number or not is one of the most asked interview question.

An armstrong number is a number that is equal to sum of cube of its own digits. Let me explain with an example, 153 is an Armstrong number. How?

armstrong number in java

Here is the case of 3 digit armstrong number,

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

Also read – palindrome number in java

Now cubes of above digits is,

(1 * 1 * 1) = 1

(5 * 5 * 5) = 125

(3 * 3 * 3) = 27

So, 1 + 125 + 27 = 153

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 whether the given number is armstrong number program in java using scanner and while loop.

import java.util.Scanner;
public class ArmstrongNumber
{
   public static void main(String[] args)
   {
      int x, y, z = 0, 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.");
      }
      sc.close();
   }
}


Output:

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


4-digit armstrong number

Now let’s learn to check armstrong number for 4 digit. Here’s the program that checks whether the given number is 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.


armstrong number in java using recursion

Let’s check for armstrong number using recursion. In java, a function that calls itself is called recursion. Here’s the complete java program to find armstrong numbers.

public class RecursionArmstrong 
{
   int num;
   int armstrongNumber(int digit, int i)
   {
      if(digit != 0)
      {
         num = digit % 10;
         i = i + (num * num * num);
         digit /= 10 ;
         return armstrongNumber(digit, i);
      }
      return i;
   }
   public static void main(String[] args) 
   {
      RecursionArmstrong obj = new RecursionArmstrong();
      int number;
      System.out.println("Armstrong numbers between 1 to 1000");
      for(int a = 1; a < 500; a++)
      {
         number = obj.armstrongNumber(a, 0);
         if(number == a)
         {
            System.out.println(a);
         }
      }
   }
}


Output:

Armstrong numbers between 1 to 1000
1
153
370
371
407


Also read – armstrong number in java for n digits


java 8

Here’s the java program.

import java.util.List;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
public class ArmstrongNumberJava8
{
   public static void main(String[] args)
   {
      List<Integer> li = ArmstrongNumberJava8.findArmstrong(5);
      System.out.println(li);
   }
   public static List<Integer> findArmstrong(int number)
   {
      return Stream.iterate(1, i -> ++i)
              .filter(i -> i == Stream.of(String.valueOf(i).split(""))
              .map(Integer::valueOf)
              .map(n -> (n * n * n))
              .mapToInt(n -> n)
              .sum())
              .limit(number)
              .collect(toList());
   }
}


Output:

[1, 153, 370, 371, 407]


armstrong number using command line arguments

Let’s learn to write a program to check whether given number is armstrong number or not.

Let’s execute below java program using command line arguments.

public class CommandLineArguments
{
   public static void main(String[] args)
   {
      int number = Integer.parseInt(args[0]);
      int input = number;
      int find = 0, remainder;
      while(number > 0)
      {
         remainder = number % 10;
         find = find + (int)Math.pow(remainder, 3);
         number = number / 10;
      }
      if(find == input)
      {
         System.out.println(input + " is an armstrong number.");
      }
      else
      {
         System.out.println(input + " is not a armstrong number.");
      }
   }
}


Output:

javac CommandLineArguments.java

java CommandLineArguments 153

153 is an armstrong number

java CommandLineArguments 568

568 is not an armstrong number.