C++ Program to Check if Number is Krishnamurti Number or Not using For Loop
Write C++ Program to Check if Number is Krishnamurti Number or Not using For Loop
// CPP Program to Check if Number is Krishnamurti Number or Not using For Loop
// Krishnamurti Number: sum of the factorial of each digit is same as the digit
#include <iostream>
using namespace std;
int fact(int n)
{
int f = 1;
for(; n > 0; )
{
f = f * n;
n--;
}
return f;
}
int main()
{
int n, ans = 0;
cout << "Enter the number:--> ";
cin >> n;
int temp = n;
for(; n != 0; )
{
int remainder = n % 10;
ans = ans + fact(remainder);
n = n / 10;
}
if(temp == ans)
{
cout << "The given number is Krishnamurti number";
}
else
{
cout << "The given number is not Krishnamurti number";
}
return 0;
}
Output:
Run 1:
Enter the number:--> 145
The given number is Krishnamurti number
Run 2:
Enter the number:--> 371
The given number is not Krishnamurti number