# C Program to Check If the Number is Krishnamurthy Number or Not

In this post, we will learn how to check whether a number is Krishnamurthy number or not using C Programming language. But before that, let’s first learn about Krishnamurthy numbers.

Krishnamurthy number is a number which is equal to the sum of the factorials of its digits. For example: 145 is a Krishnamurthy number because 145 = 1! + 4! + 5! = 1 + 24 + 120 = 145.

We will check for krishnamurthy number using the following approaches:

1. Using For Loop
2. Using While Loop

So, without further ado, let’s begin this tutorial.

## C Program to Check If the Number is Krishnamurthy Number or Not Using For Loop

```// C Program to Check If the Number is Krishnamurthy Number or Not
#include <stdio.h>
int main(){
int num, i, rem, temp, fact, sum = 0;

printf("Enter a number: ");
scanf("%d", &num);

for (temp = num; temp > 0; temp = temp/10){
fact = 1;
rem = temp % 10;

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

// Checking for krishnamurthy number
if (num == sum){
printf("%d is a krishnamurthy number.", num);
}
else{
printf("%d is not a krishnamurthy number.", num);
}
return 0;
}
```

Output 1

``````Enter a number: 145
145 is a krishnamurthy number.
``````

Output 2

``````Enter a number: 123
123 is not a krishnamurthy number.
``````

## How Does This Program Work ?

```    int num, i, rem, temp, fact, sum = 0;
```

In this program, we have declared 6 integer data type variables named num, i, rem, temp, fact and sum.

```    // Asking for input
printf("Enter a number: ");
scanf("%d", &num);
```

The user is asked to enter a number to check for krishnamurthy number.

```    for (temp = num; temp > 0; temp = temp/10){
fact = 1;
rem = temp % 10;

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

Suppose the entered number is 145, then:

1st For Loop Iteration: for (temp = 145; 145 > 0; 145 = 145/10)
fact = 1
rem = 145 % 10 = 5

Now, for (i = 1; i < = 5; i++)

• After 1st iteration: fact = 1 * 1 = 1
• After 2nd iteration: fact = 2 * 1 = 2
• After 3rd iteration: fact = 3 * 2 = 6
• After 4th iteration: fact = 4 * 6 = 24
• After 5th iteration: fact = 5 * 24 = 120

So, we get the sum = sum + fact = 0 + 120 = 120.

2nd For Loop Iteration: for (temp = 14; 14 > 0; 14 = 14/10)
fact = 1
rem = 14 % 10 = 4

Now, for (i = 1; i < = 4; i++)

• After 1st iteration: fact = 1 * 1 = 1
• After 2nd iteration: fact = 2 * 1 = 2
• After 3rd iteration: fact = 3 * 2 = 6
• After 4th iteration: fact = 4 * 6 = 24

Now, the sum = sum + fact = 120 + 24 = 144.

3rd For Loop Iteration: for (temp = 1; 1 > 0; 1 = 1/10)
fact = 1
rem = 1 % 10 = 1

Now, for (i = 1; i < = 4; i++)

• After 1st iteration: fact = 1 * 1 = 1

Finally, the sum becomes = sum + fact = 144 + 1 = 145.

```    // Checking for krishnamurthy number
if (num == sum){
printf("%d is a krishnamurthy number.", num);
}
else{
printf("%d is not a krishnamurthy number.", num);
}
```

Now, we check whether the sum of the factorial of the digits is equal to the original number or not.

If the first condition is True, then we print that the entered number is a Krishnamurthy number.

Otherwise, we print that the entered number is not a Krishnamurthy number.

## C Program to Check Krishnamurthy Number Using While Loop

```// C Program to Check Krishnamurthy Number Using While Loop
#include <stdio.h>
int main(){
int num, rem, fact, temp, sum = 0;

printf("Enter a number: ");
scanf("%d", &num);

// Assigning a temporary variable
temp = num;

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

// Finding factorial of the remainder
for (int i = 1; i <= rem; i++){
fact = i * fact;
}
sum = sum + fact;
temp = temp / 10;
}

// Checking krishnamurthy number
if (num == sum){
printf("%d is a krishnamurthy number.", num);
}
else{
printf("%d is not a krishnamurthy number.", num);
}
return 0;
}
```

Output

``````Enter a number: 40585
40585 is a krishnamurthy number.
``````

## Conclusion

I hope after going through this post, you understand how to check whether a number is a Krishnamurthy number or not using C Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.