
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Check Whether a Number is Krishnamurthy Number or Not in Java?
What is the Krishnamurthy Number?
The Krishnamurthy number is a number which the sum of the factorials of all its digits is equal to the original number. These numbers are also called the Strong number, Special number, and the Peterson number.
For example, let's take the number 40585. Now, calculate the sum of the factorials of its digits: 4! + 0! + 5! + 8! + 5!. The sum is equal to 40585. If you compare it with the original number, you can see that both are the same.
Here are some other examples of Krishnamurthy numbers, such as 1, 2, 145, 40585, etc.
Input & Output Scenarios
The following input and output scenarios will help you understand how to calculate and check the Krishnamurthy number, which you can implement in the program:
Scenario 1
Suppose the input number is 2:
Input: 2 Output: Yes Calculation: sum = 2! = 2 x 1 = 2 (i.e., equal to the original number)
Since the sum of the factorial of all digits in 2 is equal to the original number, 2 is a Krishnamurthy number.
Scenario 2
Suppose the given number is 55:
Input: 55 Output: No Calculation: sum = 5! + 5! = 120 + 120 = 245(i.e., is not equal to the original number).
The sum of the factorial of all digits in 55 is not equal to the original number, so 55 is not a Krishnamurthy number.
Example 1
The following program checks the Krishnamurthy number by comparing the sum of factorials of all digits (sum = 1! + 4! + 5!) with the original number 145:
public class checkKrishnamurthy { public static void main(String[] args) { int originalNumber = 145; System.out.println("The given number is: " + originalNumber); int copyOfOriginalNumber = originalNumber; int sum = 0; while (originalNumber != 0) { //get the last digit of the original number int digit = originalNumber % 10; int fact = 1; //find the factorial of each digit for (int i= 1; i<= digit; i++){ fact = fact * i; } //add the factorial with sum sum = sum + fact; //remove the last digit after each iteration originalNumber = originalNumber / 10; } if (sum == copyOfOriginalNumber){ System.out.println("Yes! " + copyOfOriginalNumber + " is a Krishnamurthy number"); } else{ System.out.println("No! " + copyOfOriginalNumber + " is not a Krishnamurthy number"); } } }
Below is the output of the above program:
The given number is: 145 Yes! 145 is a Krishnamurthy number
Example 2
In the following example, we define the checkKrishnamurthyNumber() method, which calls the findFactorial() method to calculate the factorial of each digit of the number 55 and then calculates the sum of the factorials of all digits (sum = !5 + !5). Here, we compare the sum with the original number to check krishnamurthy number:
public class checkKrishnamurthy{ //method to check krishnamurthy number public static boolean checkKrishnamurthyNumber(int originalNumber){ int copyOfNumber = originalNumber; int sum = 0; while(originalNumber > 0){ //get the last digit int digit = originalNumber % 10; //call the findFactorial() method to calculate factorial of a number sum = sum + findFactorial(digit); //remove the last digit after each iteration originalNumber = originalNumber / 10; } if (sum == copyOfNumber){ return true; } return false; } //method to calculate the factorial of a number public static int findFactorial(int num){ int fact = 1; for(int i = 2; i<=num; i++){ fact = fact * i; } return fact; } public static void main(String[] args){ int originalNumber = 55; System.out.println("The given number is: " + originalNumber); //calling the checkKrishnamurthyNumber() method to check the Krishnamurthy number System.out.println("Is number " + originalNumber + " is Krishnamurthy number? " + checkKrishnamurthyNumber(originalNumber)); } }
The above program produces the following output:
The given number is: 55 Is number 55 is Krishnamurthy number? false