0
0
CProgramBeginner · 2 min read

C Program to Find Strong Number with Output and Explanation

A C program to find a strong number calculates the sum of factorials of each digit and compares it with the original number using a loop and factorial function, like if (sum == num) printf("Strong number");.
📋

Examples

Input145
Output145 is a Strong number
Input123
Output123 is not a Strong number
Input1
Output1 is a Strong number
🧠

How to Think About It

To find if a number is strong, break it into digits, find each digit's factorial, add these factorials, and check if the sum equals the original number. If yes, it's a strong number.
📐

Algorithm

1
Get input number
2
Initialize sum to 0
3
For each digit in the number, find factorial and add to sum
4
Compare sum with original number
5
Print if number is strong or not
💻

Code

c
#include <stdio.h>

int factorial(int n) {
    int fact = 1;
    for (int i = 1; i <= n; i++) {
        fact *= i;
    }
    return fact;
}

int main() {
    int num, temp, digit, sum = 0;
    printf("Enter a number: ");
    scanf("%d", &num);
    temp = num;
    if (temp == 0) {
        sum = factorial(0);
    }
    while (temp > 0) {
        digit = temp % 10;
        sum += factorial(digit);
        temp /= 10;
    }
    if (sum == num)
        printf("%d is a Strong number\n", num);
    else
        printf("%d is not a Strong number\n", num);
    return 0;
}
Output
Enter a number: 145 145 is a Strong number
🔍

Dry Run

Let's trace the number 145 through the code

1

Input number

num = 145, temp = 145, sum = 0

2

Extract digit and calculate factorial

digit = 5, factorial(5) = 120, sum = 0 + 120 = 120

3

Next digit

temp = 14, digit = 4, factorial(4) = 24, sum = 120 + 24 = 144

4

Next digit

temp = 1, digit = 1, factorial(1) = 1, sum = 144 + 1 = 145

5

Compare sum with original

sum = 145, num = 145, sum == num is true

DigitFactorialSum
5120120
424144
11145
💡

Why This Works

Step 1: Calculate factorial of digits

Each digit's factorial is calculated using a loop multiplying numbers from 1 to the digit.

Step 2: Sum factorials

Sum all factorials of digits extracted from the number.

Step 3: Compare sum with original number

If the sum equals the original number, it is a strong number.

🔄

Alternative Approaches

Recursive factorial function
c
#include <stdio.h>

int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

int main() {
    int num, temp, digit, sum = 0;
    printf("Enter a number: ");
    scanf("%d", &num);
    temp = num;
    if (temp == 0) {
        sum = factorial(0);
    }
    while (temp > 0) {
        digit = temp % 10;
        sum += factorial(digit);
        temp /= 10;
    }
    if (sum == num)
        printf("%d is a Strong number\n", num);
    else
        printf("%d is not a Strong number\n", num);
    return 0;
}
Uses recursion for factorial calculation, which is elegant but may be slower for large digits.
Precompute factorials in array
c
#include <stdio.h>

int main() {
    int fact[10] = {1,1,2,6,24,120,720,5040,40320,362880};
    int num, temp, digit, sum = 0;
    printf("Enter a number: ");
    scanf("%d", &num);
    temp = num;
    if (temp == 0) {
        sum = fact[0];
    }
    while (temp > 0) {
        digit = temp % 10;
        sum += fact[digit];
        temp /= 10;
    }
    if (sum == num)
        printf("%d is a Strong number\n", num);
    else
        printf("%d is not a Strong number\n", num);
    return 0;
}
Faster because factorials are precomputed, saving repeated calculations.

Complexity: O(d) time, O(1) space

Time Complexity

The program loops through each digit once, so time depends on the number of digits d, making it O(d).

Space Complexity

Uses a fixed amount of extra space for variables and factorial calculation, so O(1).

Which Approach is Fastest?

Precomputing factorials in an array is fastest because it avoids repeated factorial calculations.

ApproachTimeSpaceBest For
Iterative factorialO(d * k)O(1)Simple and clear for small inputs
Recursive factorialO(d * k)O(k) due to recursion stackElegant but less efficient
Precomputed factorial arrayO(d)O(1)Fastest for repeated checks
💡
Precompute factorials of digits 0-9 to speed up the program.
⚠️
Forgetting to reset the temporary variable before the digit extraction loop causes wrong results.