C Program to find the given number is strong or not


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

Program

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

#include<stdio.h>
int main(){
   int n,i;
   int fact,rem;
   printf("
Enter a number : ");    scanf("%d",&n);    printf("
");    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; }

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

Updated on: 06-Nov-2023

25K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements