# How to Check Whether a Number is Krishnamurthy Number or Not in Java?

Krishnamurthy number can be defined as a number when the sum of the factorial of all digits is equal to the original number.

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

## To show you some instances

### Instance-1

Input number is 1

Let’s check it by using the logic of Krishnamurthy number −

1 = 1! = 1 which is equal to the original number.


Hence, 1 is a Krishnamurthy number.

### Instance-2

Input number is 145

Let’s check it by using the logic of Krishnamurthy number −

145 = 1!  + 4! + 5! = 1 + 24 + 120 = 145 which is equal to the original number.


Hence, 145 is a Krishnamurthy number.

### Instance-3

Input number is 55

Let’s check it by using the logic of Krishnamurthy number −

55 = 5!  +  5! = 120 + 120 = 240 which is not equal to the original number.


Hence, 55 is not a Krishnamurthy number.

Some of other examples of Krishnamurthy numbers include 2, 40585 etc.

## Algorithm

• Step 1 − Get an integer number either by initialization or by user input.

• Step 2 − Keep a copy of the original number to compare it with the new number.

• Step 3 − Find the factorial of each digit of the input number and keep a track of the sum of the factorial of the digits.

• Step 4 − At last, compare the sum of the factorial of the digits with the copy of the original input number. If both are equal then it is a Krishnamurthy number. Otherwise the input number is not a Krishnamurthy number.

## Multiple Approaches

We have provided the solution in different approaches.

• By Using Static Input Value

• By Using User Defined Method

Let’s see the program along with its output one by one.

### Approach-1: By Using User Input Value

In this approach, an integer value will be initialized in the program and then by using the algorithm we can check whether a number is a Krishnamurthy number or not.

#### Example

import java.util.*;

public class Main {
//main method
public static void main(String[] args) {
//initialized a number
int originalNumber = 145;
//printing the given number
System.out.println("Given number: "+originalNumber);

//keep a copy of original number
int copyOfOriginalNumber = originalNumber;
//initializing sum value as 0
int sum = 0;
//continue the while loop till the original number becomes 0
while (originalNumber != 0) {
//find the digit of the number
int digit = originalNumber%10;
//declare an integer variable say fact & initialize as 1
int fact = 1;
//find the factorial by using for loop
for (int i= 1; i<= digit; i++){
fact = fact * i;
}
sum = sum + fact;
//get the updated number
originalNumber = originalNumber / 10;
}
//If sum of factorials and original input number
//are equal then it is a Krishnamurthy number
if (sum == copyOfOriginalNumber)
System.out.println(copyOfOriginalNumber + " is a Krishnamurthy number");
//print it is not a Krishnamurthy number
else
System.out.println(copyOfOriginalNumber + " is not a Krishnamurthy number");
}
}


#### Output

Given number: 145
145 is a Krishnamurthy number


### Approach-2: By Using User Defined

In this approach the user will be asked to take the input of an integer value and then we will call a user defined method by passing this input number as parameter.

Inside the method we will check whether a number is a Krishnamurthy number or not by using the algorithm.

#### Example

import java.util.*;

public class Main {
//main method
public static void main(String[] args){
//initialized a number
int originalNumber = 40585;
//printing the given number
System.out.println("Given number: "+originalNumber);
//keep a copy of original number
int copyOfOriginalNumber = originalNumber;
//calling the method to check Krishnamurthy number
if (checkKrishnamurthy(originalNumber))
System.out.println(copyOfOriginalNumber + " is a Krishnamurthy number");
else
System.out.println(copyOfOriginalNumber + " is not a Krishnamurthy number");
}

//user defined method
public static boolean checkKrishnamurthy(int originalNumber){
//keep a copy of original number
int copyOfNumber = originalNumber;

//initializing sum value as 0
int sum = 0;
//continue the while loop till the original number becomes 0
while(originalNumber>0){
//find the digit
int digit=originalNumber%10;
//find factorial by calling the user defined method findFactorial()
sum=sum+findFactorial(digit);
originalNumber=originalNumber/10;
}
//If sum of factorials and original input number
//are equal then return true
if (sum == copyOfNumber)
return true;
//else return false
else
return false;
}
//find the factorial of an integer
public static int findFactorial(int num){
//declare an integer variable say fact & initialize as 1
int factorial=1;
//continue loop till number reaches 1
while(num>=1){
//multiply number with factorial
factorial=factorial*num;
//decrement the number
num--;
}
//return the factorial
return factorial;
}
}


#### Output

Given number: 40585
40585 is a Krishnamurthy number


In this article, we explored how to check a number whether it is a Krishnamurthy number or not in Java by using different approaches.