C++ Program to Check Number is Krishnamurti Number or Not using While Loop

Write C++ Program to Check Number is Krishnamurti Number or Not using While Loop

// CPP Program to Check Number is Krishnamurti Number or Not using While 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;
	while(n > 0)
	{
		f = f * n;
		n--;
	}
	return f;
}

int main()
{
	int n, ans = 0;

	cout << "Enter the number:--> ";
	cin >> n;

	int temp = n;
	while(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

≪ Previous | Next ≫