Home » C program to check the number is Strong number or not

C program to check the number is Strong number or not

Learn about C program to check the number is Strong number or not in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

C program to check the number is Strong number or not

Strong numbers are ones in which the sum of the factorials of each digit equals the original number. For example, 1 is a strong number because 1!=1, 2 is a strong number because 2!=2, 145 is a strong number because 1! + 4! + 5! = 1 + 24 + 120 = 145, and so on.

Program:

#include<stdio.h>
int main()
{
     int number, tempvariable, remainder, i, factorial, sum=0;

     printf("Enter number: ");
     scanf("%d",&number);

     tempvariable = number;

     while( number!=0 )
     {
         factorial = 1;  //every time factorial initialize with 1
         remainder = number%10;

         for(i=1; i<=remainder; i++)
         {
             factorial *= i; //factorial=factorial * i
         }

         sum += factorial; 
         number /= 10;  
     }

     if( tempvariable == sum )
         printf("%d is a strong number.\n",tempvariable);
     else
         printf("%d is not a strong number.\n",tempvariable);

     return 0;
}

Output:

Enter number: 145
145 is a strong number.

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
Find Prime Number in a Given Range
Prime number program in C using while loop