C program to Check the Number is a Krishnamurthy Number

Write a C program to check the number is a Krishnamurthy number using for loop. First, if the numbers equal the sum of factors of individual digits of it, it is a Krishnamurthy number. This C example divides the number into individual digits and finds the sum of factors of each digit. Next, if condition checks whether the sum equals the actual number and is true, it is a Krishnamurthy number.

#include <stdio.h>
int main()
{
  long fact;
  int Number, tempNum, rem, Sum = 0, i;
  
  printf("Enter Number to Check for Krishnamurthy Number = ");
  scanf("%d", &Number);

  for (tempNum = Number; tempNum > 0; tempNum = tempNum / 10)
  {
    fact = 1;

    rem = tempNum % 10;

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

    Sum = Sum + fact;
  }

  if (Number == Sum)
    printf("\n%d is a Krishnamurthy Number.\n", Number);
  else
    printf("\n%d is not a Krishnamurthy Number.\n", Number);
}
C program to Check the Number is a Krishnamurthy Number

This C program checks whether the given number is a Krishnamurthy number or not using a while loop.

#include <stdio.h>
int main()
{
  long fact;
  int Number, tempNum, rem, Sum = 0, i;

  printf("Enter Number to Check for Krishnamurthy Number = ");
  scanf("%d", &Number);

  tempNum = Number;
  while (tempNum > 0)
  {
    fact = 1;
    i = 1;
    rem = tempNum % 10;
    while (i <= rem)
    {
      fact = fact * i;
      i++;
    }
    Sum = Sum + fact;
    tempNum = tempNum / 10;
  }
  if (Number == Sum)
    printf("\n%d is a Krishnamurthy Number.\n", Number);
  else
    printf("\n%d is not a Krishnamurthy Number.\n", Number);
}
Enter Number to Check for Krishnamurthy Number = 40585

40585 is a Krishnamurthy Number.


Enter Number to Check for Krishnamurthy Number = 2248

2248 is not a Krishnamurthy Number.

In this C example, the Calculate_fact returns factorial of a number, and the Krishnamurthy_Number divides the number into digits finds the sum of factorial. Finally, this c program prints the Krishnamurty numbers from 1 to N or in a given range.

#include <stdio.h>

long Calculate_fact(int Number)
{
  if (Number == 0 || Number == 1)
    return 1;
  else
    return Number * Calculate_fact(Number - 1);
}

int Krishnamurthy_Number(int Number)
{
  int rem, Sum = 0;
  long fact;
  for (; Number > 0; Number = Number / 10)
  {
    fact = 1;
    rem = Number % 10;
    fact = Calculate_fact(rem);
    Sum = Sum + fact;
  }
  return Sum;
}

int main()
{
  int Number, Sum = 0, min, max;

  printf("Please Enter the min & max Krishnamurthy Number = ");
  scanf("%d %d", &min, &max);

  printf("The List of Krishnamurthy Numbers\n");
  for (Number = min; Number <= max; Number++)
  {
    Sum = Krishnamurthy_Number(Number);
    if (Number == Sum)
    {
      printf("%d  ", Number);
    }
      
  }

  printf("\n");
}
Please Enter the min & max Krishnamurthy Number = 1 100000
The List of Krishnamurthy Numbers
1  2  145  40585