Home » C Program to Print All the Krishnamurthy Numbers from 1 to N

C Program to Print All the Krishnamurthy Numbers from 1 to N

Learn about C Program to Print All the Krishnamurthy Numbers from 1 to N in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

C Program to Print All the Krishnamurthy Numbers from 1 to N

It’s a number that’s equal to the sum of all its digits’ factorials. For instance, 1, 2, and 145. It is also known as a Strong number or a Peterson number. In the below code, we will check a Krishnamurthy Number program in C.

Program:

#include<stdio.h>
int main()
{
  int number, temp, sum, currentDigit, fact;
  printf("Enter an Integer: ");
  scanf("%d",&number);
  temp = number;
  sum = 0;

  while(temp!=0)
  {
    currentDigit = temp % 10;
    fact = 1;

    for(int i=1; i<=currentDigit; i++)
    {
      fact *= i;
    }

    sum += fact;
    temp /= 10;
  }

  if(sum == number)
  {
    printf("%d is Krishnamurthy Number.",number);
  }
  else
  {
    printf("%d is not a Krishnamurthy Number.",number);
  }

  return 0;
}

Output:

Enter an Integer: 145
145 is Krishnamurthy Number.

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
C Program to Find Krishnamurthy Number
Strong number in a given range