C Program to find the given number is strong or not

CServer Side ProgrammingProgramming

A strong number is a number, where the sum of the factorial of the digits is equal to the number itself.

Example

  • 123!= 1!+2!+3!

                   =1+2+6 =9

Here, 123 is not a strong number because, the sum of factorial of digits is not equal to the number itself.

  • 145!=1!+4!+5!

            =1+24+120

            =145

Here, 145 is a strong number because, the sum of factorial of digits is equal to the number itself.

The logic we use to find whether the given number is strong or not is as follows −

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\n",temp);
else
   printf("%d is not a strong number\n",temp);

Program

Following is the C program to find whether the given number is strong or not −

 Live Demo

#include<stdio.h>
int main(){
   int n,i;
   int fact,rem;
   printf("\nEnter a number : ");
   scanf("%d",&n);
   printf("\n");
   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\n",temp);
   else
      printf("%d is not a strong number\n",temp);
   return 0;
}

Output

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

Run 1:
Enter a number : 145
145 is a strong number
Run 2:
Enter a number : 25
25 is not a strong number
raja
Published on 12-Mar-2021 07:01:58
Advertisements