# C program to check if a number is Strong number

Category: C Program

Learn how to write a C program to check if a number is a Strong number. This guide includes an explanation of Strong numbers, a step-by-step algorithm, and complete code with examples.

A Strong number (also known as a Krishnamurthy number) is a special number whose sum of the factorials of its digits is equal to the number itself. For example, 145 is a Strong number because 1! + 4! + 5! = 1 + 24 + 120 = 145.

In this article, we will write a C program to check whether a given number is Strong. We'll begin by understanding the concept of Strong numbers, then outline the algorithm, and finally provide the complete C program with detailed explanations.

## Understanding Strong Numbers

A Strong number satisfies the following condition:

Sum of the factorials of its digits=Number

For example:

• 145 is a Strong number because 1! + 4! + 5! = 145.

## Algorithm to Check for a Strong Number

1. Input the number: Get the number from the user.
2. Find the factorial of each digit: Iterate through each digit of the number and find its factorial.
3. Sum the factorials: Calculate the sum of the factorials of all digits.
4. Compare the sum with the original number: If the sum equals the original number, it is a Strong number; otherwise, it is not.

## Write a C program to check if a number is Strong number

Here is the complete C program to check whether a given number is a Strong number or not:

``````#include <stdio.h>

int main() {
int num, originalNum, remainder, sum = 0;

// Input the number from the user
printf("Enter an integer: ");
scanf("%d", &num);

// Store the original number to compare later
originalNum = num;

// Calculate the sum of the factorials of each digit
while (num > 0) {
remainder = num % 10;

// Calculate factorial of the remainder
int fact = 1;
for (int i = 1; i <= remainder; i++) {
fact *= i;
}

sum += fact;
num /= 10;
}

// Check if the sum of the factorials is equal to the original number
if (sum == originalNum) {
printf("%d is a Strong number.", originalNum);
} else {
printf("%d is not a Strong number.", originalNum);
}

return 0;
}
``````

Output

``````Enter an integer: 40585
40585 is a Strong number.
``````

## Explanation of the Code

1. Input the Number:
• `scanf("%d", &num);` reads the integer input from the user.
2. Store the Original Number:
• `originalNum = num;` stores the original number to compare later.
3. Calculate the Sum of the Factorials of Each Digit:
• The `while` loop iterates through each digit of the number.
• `remainder = num % 10;` extracts the last digit.
• The `for` loop calculates the factorial of the digit.
• `sum += fact;` adds the factorial of the digit to the sum.
• `num /= 10;` removes the last digit from the number.
4. Check for Strong Number:
• The condition `if (sum == originalNum)` checks if the sum of the factorials is equal to the original number. If true, it prints that the number is a Strong number; otherwise, it states that it is not.

## Example Runs

Let's look at some example runs to see how the program works:

### Example 1

``````Enter an integer: 145
145 is a Strong number.
``````
• Calculation: 1! + 4! + 5! = 1 + 24 + 120 = 145.

### Example 2

``````Enter an integer: 123
123 is not a Strong number.
``````
• Calculation: 1! + 2! + 3! = 1 + 2 + 6 = 9.

If you found this article helpful, you might also be interested in learning how to calculate factorial of a number. Check out our detailed guide below: