C Program to Check Number is Krishnamurthy Number using While loop

C Program to Check Number is Krishnamurthy Number using While loop

Write C Program to Check Number is Krishnamurthy Number using While loop

// Write a C program to check the number is krishnamurti number or not
// Krishnamurti Number: sum of the factorial of each digit is same as the digit

#include <stdio.h>

int fact(int n)
{
	int f = 1;
	while(n > 0)
	{
		f = f * n;
		n--;
	}
	return f;
}

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

	printf("Enter the number:--> ");
	scanf("%d", &n);

	int temp = n;
	while(n != 0)
	{
		int remainder = n % 10;
		ans = ans + fact(remainder);
		n = n / 10;
	}
	if(temp == ans)
	{
		printf("The given number is Krishnamurti number");
	}
	else
	{
		printf("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