Strong Numbers in a Given Range in C

Strong Number in a Given Range

Today in this page we will be discussing the code to find the Strong Numbers in a Given Range in C  programming language.

Strong number is a special number whose sum of the factorial of digits is equal to the original number.

For Example: 145 is strong number. Since, 1! + 4! + 5! = 145.

Strong Numbers in a Given Range in C

The idea is to iterate from (1, N)  and check if any number between the range is a strong number or not. If yes then print the corresponding number, else we need to check for the next number.
Let’s understand this with help of some examples

Input: N = 100 
Output: 1 2 
Explanation: 
Only 1 and 2 are the strong numbers from 1 to 100 because 
1! = 1, and 
2! = 2

Input: N = 1000 
Output: 1 2 145 
Explanation: 
Only 1, 2 and 145 are the strong numbers from 1 to 1000 because 
1! = 1, 
2! = 2, and 
(1! + 4! + 5!) = 145 

Strong Numbers in a Given Range C Code

Run
#include <stdio.h>
int main() {
    int i, n, n1, s1 = 0, j, k, en, sn;
    long fact;

    printf("Find Strong Numbers within an range of numbers:\n");
    /* If sum of factorial of digits is equal to original number then it is Strong number */
    sn = 1;
    en = 150;

    for (k = sn; k <= en; k++) {
        n1 = k;
        s1 = 0;

        for (j = k; j > 0; j = j / 10) {
            fact = 1;

            for (i = 1; i <= j % 10; i++) {
                fact = fact * i;
            }
            s1 = s1 + fact;
        }
        if (s1 == n1) printf("%d  ", n1);
    }
    printf("\n");
    return 0;
}
Find Strong Numbers within an range of numbers:
1 2 145