Print The First n strong Numbers in C

First n strong Numbers

Today in this page we will be discussing the code to print First n strong Numbers in C programming language.

A strong number is a special number whose sum of the factorial of digits is equal to the original number. For Example, 145 is a strong number. Since, 1! + 4! + 5! = 145.

Print The First n strong Numbers 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 145 
Explanation: 
Only 1, 2 and 145 are the strong numbers from 1 to 100 because 
1! = 1, 
2! = 2, and 
(1! + 4! + 5!) = 145 

Print The First n strong Numbers C Code

Run
#include <stdio.h>
#include <stdlib.h>

int factorial(int f) {
    int mul = 1;
    for (int i = 1; i <= f; i++) {
        mul = mul * i;
    }
    return mul;
}
int main() {
    int fact = 1, sum = 0;
    int n, r;
    n=200;
    printf("Strong numbers are :");
    for (int i = 1; i <= n; i++) {
        int k = i;
        while (k != 0) {
            r = k % 10;
            fact = factorial(r);
            k = k / 10;
            sum = sum + fact;
        }
        if (sum == i) {
            printf("%d, ", i);
        }
        sum = 0;
    }
    return 0;
}
Strong numbers are :1, 2, 145,