Strong Number Program in Java

If the sum of the factorial of individual digits of a number is equal to the same number then the number is called a strong number. In this post, we will develop a strong number program in Java.

Example:-
145 = 1! + 4! + 5! = 1 + 24 + 120 = 145
So, 145 is a strong number.

234 = 2! + 3! + 4! = 2 + 6 + 24 = 32
So, 234 is not a strong number.

40585 = 4! + 0! + 5! + 8! + 5! = 24 + 1 + 120 + 40320 + 120 = 40585
So, 40585 is a strong number.

Procedure to develop a method to check number is a strong number or not
1) Take a number as input
2) Take a sum variable and initialize it with 0
3) Find the last digit of the number
4) Calculate factorial of that last digit
5) Add factorial result in the sum
6) remove the last digit from the number
7) Repeat steps 3 to 6 until the number becomes 0.
8) If the sum is equal to the input (number) then it is a strong number

The Java method to check the number is a strong number or not, can be written as below:-

public static boolean isStrong(int number) {

   // declare variables
   int sum = 0, lastDigit = 0;

   //store number in a temporary variable
   int tempNum = number;

   // traverse through all digits of number
   while(tempNum != 0) {

       // find last digit
       lastDigit = tempNum % 10;

       // calculate factorial and 
       // add it to the sum
       sum += factorial(lastDigit);

       // remove last digit
       tempNum /= 10;
   }

   // compare sum and number
   if(sum == number)
       return true; // strong number
   return false; // not a strong number
}

We are storing the actual number in a temporary variable because at last, we need to compare the result (sum) with the actual number. If the result(sum) is equal to the actual number then the number is a strong number else it is not a strong number.

Also see:- Special numberMagic numberArmstrong numberPerfect numberEvil NumberSpy NumberSunny number in Java

Java program to check strong number

Now based on the above method we will develop a strong number program in Java programming language. We will develop isStrong(-) method to check the number is a strong number or not and the factorial(-) method to find the factorial of a number.

import java.util.Scanner;

public class StrongNumber {

   // Checks the number is strong number or not
   public static boolean isStrong(int number) {

      // declare variables
      int sum = 0, lastDigit = 0;
      int tempNum = number;

      // traverse through all digits of number
      while(tempNum != 0) {
          lastDigit = tempNum % 10;
          sum += factorial(lastDigit);
          tempNum /= 10;
      }

      // compare sum and number
      if(sum == number)
          return true; 
      return false; 
   }

   // calculate factorial of an integer
   public static long factorial(int n) {
      long fact = 1;
      for(int i=1; i<=n; i++) {
          fact *= i;
      }
      return fact;
   }

   public static void main(String[] args) {
      // declare variables
      int number = 0;
      boolean result = false;

      //create Scanner class object to take input
      Scanner scan = new Scanner(System.in);

      // take input from end-user
      System.out.print("Enter an integer number:: ");
      number = scan.nextInt();

      // check number is strong number or not
      result = isStrong(number);
      if(result)
          System.out.println(number +
                  " is a strong number.");
      else
          System.out.println(number +
                 " is not a strong number");

      // close Scanner class object
      scan.close();
   }
}

The output for different test-cases:-

Enter an integer number:: 145
145 is a strong number.

Enter an integer number:: 146
146 is not a strong number

Optimization

We can analyze that the last digit will be always from 0 to 9, and every time we need to find the factorial from 0 to 9 only. So, it is a better idea to calculate the factorial value from 0 to 9 and store it in an array. For the large numbers, It will be an optimized solution for checking the number is a strong number or not. Conclusion:- before checking the number is a strong number or not, calculate factorial from 0 to 9 and store it in an array. It can be done as below:-

// Array to store factorial value 
// from 0 to 9
static int fact[] = new int[10];

// static block to calculate factorial
static {
   // factorial of 0 and 1 is 1
   fact[0] = fact[1] = 1;

   // factorial is also calculated as
   // n! = (n-1)! * n
   for(int i=2; i<fact.length; i++)
       fact[i] = fact[i-1] * i;
}

The static block executes before executing the main method so, the factorial values from 0 to 9 will be calculated and stored in the array fact[]. Now, inside the isStrong() method for the factorial value use this array as,

// add factorial value of last digit
// to the sum variable
sum += fact[lastDigit];

In the below Java program, we used this optimized solution to check the number is a strong number or not.

Optimized program to check number is Strong number or not

import java.util.Scanner;

public class StrongNumber {

   // Array to store factorial value 
   // from 0 to 9
   static int fact[] = new int[10];

   // static block to calculate factorial
   static {
      fact[0] = fact[1] = 1;
      for(int i=2; i<fact.length; ++i)
         fact[i] = fact[i-1] * i;
   }
    
   // Checks the number is strong number or not
   public static boolean isStrong(int number) {

      // declare variables
      int sum = 0, lastDigit = 0;
      int tempNum = number;

      // traverse through all digits of number
      while(tempNum != 0) {
         lastDigit = tempNum % 10;
         sum += fact[lastDigit];
         tempNum /= 10;
      }

      // compare sum and number
      return (sum == number);
   }

   // main method
   public static void main(String[] args) {

      // declare variables
      int number = 0;
      boolean result = false;

      //create Scanner class object and take input
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter an integer number:: ");
      number = scan.nextInt();

      // check number is strong number or not
      result = isStrong(number);
      if(result)
         System.out.println(number +
                       " is a strong number.");
      else
         System.out.println(number +
                    " is not a strong number");

      // close Scanner class object
      scan.close();
   }
}

Find strong number in Java within a given range

We can also find all the strong numbers in Java within a given range. For this purpose, we need to take the help of loops. We will take range values as minRange and maxRange. Later we will find all strong number which belongs to this range.

import java.util.Scanner;

public class StorngNumberInRange {

   // Array to store factorial value 
   // from 0 to 9
   static int fact[] = new int[10];

   // static block to calculate factorial
   static {
      fact[0] = fact[1] = 1;
      for(int i=2; i<fact.length; ++i) {
          fact[i] = fact[i-1] * i;
      }
   }

   // Checks the number is strong number or not
   public static boolean isStrong(int number) {
      // declare variables
      int sum = 0, lastDigit = 0;
      int tempNum = number;

      // traverse through all digits of number
      while(tempNum != 0) {
          lastDigit = tempNum % 10;
          sum += fact[lastDigit];
          tempNum /= 10;
      }

      // compare sum and number
      return (sum == number); 
   }

   public static void main(String[] args) {
      // declare variables
      int minRange = 0, maxRange = 0;

      //create Scanner class object to take input
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter the minimum value of range:: ");
      minRange = scan.nextInt();
      System.out.print("Enter the maximum value of range:: ");
      maxRange = scan.nextInt();

      // loop
      System.out.println("The Strong number from "+ 
              minRange + " to "+ maxRange+" are: ");
      for(int i=minRange; i<=maxRange; i++) {
          // check number
          if(isStrong(i))
              System.out.print(i +" ");
      }

      // close Scanner class object
      scan.close();
   }
}

Output:-

Enter the minimum value of range:: 1
Enter the maximum value of range:: 1000000
The Strong number from 1 to 1000000 are:
1 2 145 40585

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply