Updated March 31, 2023
Introduction to Krishnamurthy Number
The Krishnamurthy number is defined as a number which gets by doing sum of the factorial of individual digits is equal to the number itself, in Java, this number is also called as a special number and strong number, it can be ‘N’ digit number, it is calculated by breaking the number into its corresponding digits and then to find factorial of digits if the factorial is same as the number then that is a Krishnamurthy number, the factorial of a number means it is the multiplication of all digits from 1 to that number and factorial is represented by an exclamation mark(!). So in this topic, we are going to learn bout Krishnamurthy numbers.
The logic behind Krishnamurthy number
As we see the definition of Krishnamurthy number above, so if the sum of the factorial of a number is equal to the original number, then the number is called a special number. So let us see how we will get that number and what the logic behind them.
The logic behind these numbers is that, for getting a strong number, we have to follow some rules that are first we have to break that number, then calculate factorial of each digit from the number, after that sum up the factorials which we get, and we will also get a number, if that number is exact same to the previous number then it is a Krishnamurthy number or strong number, but if that number does not match with the previous number, then it will not be a Krishnamurthy number.
Let us see an illustration of the following numbers,
- 1 = 1! = 1, in this case, the factorial of 1(one) is 1(one) itself, and it matches the previous number; hence it is a number.
- 2 = 2*1 = 2, also in this, the factorial of 2 is 2 itself, and it matches the previous number; hence it is a Krishnamurthy number.
- 145, it has three digits, so we have to break the number and calculate the factorial of each.
1! = 1 = 1
4! = 4*3*2*1 = 24
5! = 5*4*3*2*1 = 120
Addition of factorial = 1+24+120 = 145
We get a new number that is also the same as the previous number hence it is also a Krishnamurthy number.
- 40585, has five digits so let us break them and calculate factorial for them,
4! = 4*3*2*1 = 24
0! = 1 = 1
5! = 5*4*3*2*1 = 120
8! = 8*7*6*5*4*3*2*1 = 40320
5! = 5*4*3*2*1 = 120
By adding factorials, 24+1+120+40320+120 = 40585, so that this number is similar to the previous number; hence it is also a number.
From the above points or explanation of numbers, we can understand the logic behind the Krishnamurthy number.
How to check the Krishnamurthy number?
If the sum of factorial digits of numbers from the original number is equal to the number itself, then that number is said to be a number.
Following are few steps which we can use to check whether the number is a number or not.
- First, take any number
- Then break that number into digit
- Then find the factorial of each digit
- After that, add the factorial of each digit and store that in another variable.
- If the sum of the factorial of each digit equals the original number, then that number is a Krishnamurthy number.
For example:
- Let us take a number 145, it has three digits 1, 4, and 5, and we have to calculate the factorial for each digit as follow,
The factorials are,
1! = 1 = 1
4! = 4*3*2*1 = 24
5! = 5*4*3*2*1 = 120
The factorial of 1 is 1, the factorial of 4 is 24, and factorial of 5 is 120, so if we sum up these factorials as,
1+24+120 = 145, the sum-up of factorial is the exact same as our previous number, so hence we can say that it is a Krishnamurthy number.
- Now with another number 36, it has two digits, 3 and 6, so first, we will find out factorials of 3 and 6,
3! = 3*2*1 = 6
6! = 6*5*4*3*2*1 = 720
We get factorials as 6 and 720 when we sum up this factorial as 6+720 = 726, which is not equal to the previous number 36, so in this case, we can say that such number is not a number or a strong number.
Examples of Krishnamurthy Number
Different examples are mentioned below:
Example #1
Code:
import java.util.*;
import java.io.*;
import java.util.Scanner;
class Knumber
{
static int facto(int number)
{
int j = 1;
while (number != 0)
{
j = j * number;
number--;
}
return j;
}
static boolean checkNumb(int number)
{
int add = 0;
int tempNum = number;
while (tempNum != 0)
{
add = add + facto(tempNum % 10);
tempNum = tempNum / 10;
}
if(add == number)
return true;
else
return false;
}
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter any number:");
n = sc.nextInt();
if (checkNumb(n))
System.out.println(n + " is a krishnamurthy number");
else
System.out.println(n + "This is not a krishnamurthy number");
}
}
Output:
The above program we have written to check input number is Krishnamurthy number or not. After compilation, we have entered any number; it will show that number is Krishnamurthy or not.
Example #2
Code:
import java.util.*;
import java.io.*;
import java.util.Scanner;
class FindKnumber
{
public static void main(String args[])
{
int range;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of range :");
range = sc.nextInt();
for(int i = 1; i <= range; i++)
checkNum(i);
}
static int fact(int number)
{
int f = 1;
while (number != 0) {
f = f * number;
number--;
}
return f;
}
static void checkNum(int number)
{
int add = 0;
int tempNumb = number;
while (tempNumb != 0)
{
add = add + fact(tempNumb % 10);
tempNumb = tempNumb / 10;
}
if(add == number)
System.out.println(number + " is a krishnamurthy number");
}
}
Output:
The above program is to find numbers from the given range; after compilation, we have to enter the value range then the output will be the numbers between that range.
Conclusion
In this article, we conclude that the Krishnamurthy number is also known as a strong number and special number, or it can also be called a Peterson number, as we find only three strong numbers from 1 to 1000, to find the number it has the factorial concept and some logic behind it.
Recommended Articles
This is a guide to Krishnamurthy Number. Here we discuss How to check the Krishnamurthy number along with the examples and outputs. You may also have a look at the following articles to learn more –