Special Number in Java

In this post, we will write a Java program to check whether the given number is a Special Number or not? We will also develop a Java program to display all the Special number which exists in the given range/interval.

If the sum of the factorial of all digits of a number is equal to the original number then the number is called Special Number. For example:- 145 and 40585 are Krishnamurthy numbers.

145
=> 1! + 4! + 5!
=> 1 + 24 + 120
=> 145
The sum of the factorial of individual digits is the same as the original number 145. Hence, 145 is a Special number.

Similarly,
40585
=> 4! + 0! + 5! + 8! + 5!
=> 24 +1 + 120 + 40320 + 120
=> 40585
So, 40585 is also a Special number

1! = 1
So, 1 is also a Special number.

Similarly,
2! = 1*2 = 2
2 is also a Special number.

Hence the numbers 1, 2, 145 and 40585 are the examples of Special number.

Sometimes the Special number is also called a Strong number, Krishnamurthy number, and Peterson number.

Procedure to develop a method to check the number is a Special 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 Special number

import java.util.Scanner;

public class SpecialNumber {

   // method to Check the Special number
   public static boolean isSpecial(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; 
   }

   // method to 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;
   }

   // main method
   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 Special number or not
      result = isSpecial(number);

      // display result
      if(result)
          System.out.println(number +
                  " is a Special number.");
      else
          System.out.println(number +
                 " is not a Special number.");

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

The output for the different test cases are:-

Enter an integer number:: 1
1 is a Special number.

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

Enter an integer number:: 200
200 is not a Special number.

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

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 Special number or not. Conclusion:- before checking the number is a Special number or not, calculate factorial from 0 to 9 and store it in an array.

The static block executes before executing the main method so, the factorial values from 0 to 9 should be calculated and stored in the array fact[].

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

import java.util.Scanner;

public class SpecialNumber {

    // 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;
    }

    // method to Check Special number
    public static boolean isSpecial(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 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 Special number or not
      result = isSpecial(number);

      // display result
      if(result)
          System.out.println(number +
                  " is a Special number.");
      else
          System.out.println(number +
                 " is not a Special number.");

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

Java program to find all Special number in a Range

import java.util.Scanner;

public class SpecialNumberInRange {

    // 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;
    }

    // method to Check Special number
    public static boolean isSpecial(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 minRange = 0, maxRange = 0;

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

        // loop
        System.out.println("The Special number from "+ 
                minRange + " to "+ maxRange+" are: ");

        for(int i=minRange; i<=maxRange; i++) {
            // check number
            if(isSpecial(i))
                System.out.print(i +" ");
        }

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

Output:-

Enter minimum value of range:1
Enter maximum value of range:1000000
The Special 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!

1 thought on “Special Number in Java”

  1. I Fashion Styles

    Somebody essentially help to make severely posts I would state. That is the very first time I frequented your website page and to this point? I amazed with the analysis you made to create this actual put up incredible. Excellent job!

Leave a Comment

Your email address will not be published. Required fields are marked *