Special Number Program in Java with Examples

In this post I will be sharing what is Special number, examples of Special number, algorithm and java program to check whether a given number is Special number or not.

Special number is also known as Krishnamurthy number and Strong number.

Read Also :  Happy Number Program in Java

What is Special Number

A number is known as Special number when sum of the factorial of digits is equal to the original number (given number).

Examples:

Below are examples of numbers which are Special

Number to check : 145 
1! + 4! + 5! = 1 + 24 + 120 = 145   // 145 is a Special Number
 
Number to check : 2 
2!  = 2 = 2   // 2 is a Special Number

Below are examples of numbers which are NOT Special number

Number to check : 35 
3! + 5! = 6 + 120 = 126   // 35 is NOT a Special Number
 
Number to check : 105 
1! + 0! + 5! = 1 + 1 + 120 = 122   // 105 is NOT a Special Number

Read Also :  Disarium Number Program in Java

Java Program for Special Number


import java.util.*;

public class JavaHungry {

public static void main(String args[]) {
System.out.println("Enter any number : ");
Scanner scan = new Scanner(System.in);
int inputNumber = scan.nextInt();
boolean result = specialnumber(inputNumber);
if (result == true)
System.out.println(inputNumber + " is a Special number");
if (result == false)
System.out.println(inputNumber + " is NOT a Special number");
}

public static boolean specialnumber(int inputNumber) { 
 
        // Create a copy of the inputNumber 
        int temp = inputNumber;
 
        // Initialize sumOfDigits of inputNumber
int sumOfDigits = 0; 
 
        /* Calculate the sum of factorial of
           digits */ 
        while (temp != 0) { 
           // Get the rightmost digit 
            int currentDigit = temp % 10;
sumOfDigits = sumOfDigits + factorial(currentDigit);
temp = temp/10;
} 
        /* If sumOfDigits is equal to inputNumber then 
the number is Special, otherwise not */
        return (sumOfDigits == inputNumber);
}

public static int factorial(int number) {
if (number == 1 || number == 0)
return 1;
else
return (number * factorial(number -1));
}
}

Output :
Enter any number : 145
145 is a Special number

Algorithm for Special Number

1. Create a copy of the inputNumber (original number) by storing its value in variable temp.

2. Initialize sumOfDigits variable value to 0. It will represent the sum of factorial of digits.

3. Using while loop
    a. Get the rightmost digit of variable temp by using (temp %10). Store its value in variable currentDigit.
    b. Calculate the factorial of the currentDigit .
    c. add the value of factorial method to sumOfDigits variable.

4. Check whether the variable sumOfDigits is equal to inputNumber (original number).
     a. If both are equal then the inputNumber is Special.
     b. Otherwise, the inputNumber is not a Special number.

That's all for the day, please mention in comments if you know any other way to implement Special number program in java.

0 Comments