C Program for Strong Number

In this tutorial, we will learn about the strong number and write a C Program to find Strong Number.

Strong Number

Strong numbers are those numbers whose sum of the factorial of the individual digits is equal to the number itself.

Example: 145 is a strong number.

Explanation: 145
1! + 4! + 5!
= 1 + 24 + 120
= 145, number itself.
Hence it is a Strong number.

Let us understand the Strong number in C program.


C program to check whether a number is Strong number or not

#include<stdio.h>
 
int main()
{
 int num, rem, temp;
 int  i, sum = 0, fact;
 
 printf("Enter the number to be checked: ");
 scanf("%d", &num);
 
 temp = num;
 
 while(temp)
 {
     i = 1, fact = 1;
     rem = temp%10;
 
     //calculation of factorial
     while(i <= rem)
     {
        fact = fact * i;
        i++;
     }
 
     sum += fact;
     temp /= 10;
 
}
 
 // displaying according to condition
 if(sum == num)
     printf("%d is a strong number.", num);
 else
     printf("%d is not a strong number.", num);
 
 return 0;
 
}

Output:

Enter the number to be checked: 145
145 is a strong number.


Let us go through another example where we display all the strong numbers within an entered range.

C Program to print strong numbers in a given range

// program to find strong numbers within a given range in C

#include <stdio.h>

int main()
{
   int lrRange, upRange, rem, temp;
   int i, sum = 0, fact;

   printf("Enter the lower range: ");
   scanf("%d", &lrRange);

   printf("Enter the upper range: ");
   scanf("%d", &upRange);

   printf("Strong number between entered range are: ");
   for (i = lrRange; i <= upRange; i++)
   {
      temp = i;
      while (temp)
      {
         fact = 1;
         rem = temp % 10;

         //calculation of factorial
         int count = 1;
         while (count <= rem)
         {
            fact = fact * count;
            count++;
         }

         sum += fact;
         temp /= 10;

      }

      //Display
      if (sum == i)
         printf("%d ", i);

      sum = 0;
   }

   return 0;

}

Output:

Enter the lower range: 1
Enter the upper range: 300
Strong number between entered range are: 1 2 145