# Check if a number is a Krishnamurthy Number or not in C++

C++Server Side ProgrammingProgramming

Here we will see how to check a number is Krishnamurty number or not. A number is Krishnamurty number, if the sum of the factorial of each digit is the same as the number. For example, if a number is 145, then sum = 1! + 4! + 5! = 1 + 24 + 120 = 145. So this is a Krishnamurty number,

The logic is simple, we have to find the factorial of each number, and find the sum, then if that is the same as a given number, the number is Krishnamurty number. Let us see the code to get a better idea.

## Example

Live Demo

#include <iostream>
#include <cmath>>
using namespace std;
long factorial(int n){
if(n <= 1){
return 1;
}
return n * factorial(n - 1);
}
bool isKrishnamurty(int number) {
int temp = number;
int sum = 0;
while(number > 0){
sum += factorial(number % 10);
number /= 10;
}
if(sum == temp){
return true;
}
return false;
}
int main() {
int n = 145;
if(isKrishnamurty(n)){
cout << n << " is Krishnamurty Number";
} else {
cout << n << " is not Krishnamurty Number";
}
}

## Output

145 is Krishnamurty Number
Published on 21-Oct-2019 10:14:49