Find if a Given Number is Strong in C



Recursive Factorial Calculation

A strong number is a number, where the sum of the factorial of its digits equals the number itself. In C programming, a factorial is the product of all positive integers less than or equal to a given number.

To calculate the factorial of a number, we use a recursive function with the argument N. This function will repeatedly call itself with a decremented value of N, multiplying the results until it reaches 1.

123!= 1!+2!+3!
 =1+2+6 =9

In this case, 123 is not a strong number because the sum of the factorial of its digits does not equal the number itself.

145!=1!+4!+5!

Here, 145 is a strong number because the sum of the factorials of its digits equals the number itself.

Factorial Sum Calculation

To determine if a number is strong, this C program calculates the sum of the factorials of its digits and compares it to the original number.

#include <stdio.h>

int main() {
    int n, temp, rem, sum = 0, i, fact;
    n=2271;
    temp = n;

    while (n) {
        i = 1;
        fact = 1;
        rem = n % 10;
        while (i <= rem) {
            fact = fact * i;
            i++;
        }
        sum = sum + fact;
        n = n / 10;
    }

    if (sum == temp)
        printf("%d is a strong number
", temp); else printf("%d is not a strong number
", temp); return 0; }

Program for Recursive Factorial Calculation

Below is a C program that determines if a given number is strong by calculating the sum of the factorials of its digits and comparing it to the original number, then printing the result.

#include <stdio.h>
int main() {
   int n, i;
   int fact, rem;
   n=2271;
   int sum = 0;
   int temp = n;
   while (n) {
      i = 1, fact = 1;
      rem = n % 10;
      while (i <= rem) {
         fact = fact * i;
         i++;
      }
      sum = sum + fact;
      n = n / 10;
   }
   if (sum == temp)
      printf("%d is a strong number", temp);
   else
      printf("%d is not a strong number", temp);
   return 0;
}

When the above program is executed, it produces the following result ?

2271 is not a strong number
Updated on: 2025-01-23T10:53:45+05:30

40K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements