C Program to Check if Number is Krishnamurti Number or Not using For Loop

C Program to Check if Number is Krishnamurti Number or Not using For Loop

Write a Program to Check if Number is Krishnamurti Number or Not using For Loop

// C 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 <stdio.h>

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

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

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

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

Enter the number:--> 145
The given number is Krishnamurti number

Enter the number:--> 371
The given number is not Krishnamurti number