Home  >  Article  >  Backend Development  >  C program to determine whether a given number is a strong number

C program to determine whether a given number is a strong number

PHPz
PHPzforward
2023-09-09 13:25:021342browse

C program to determine whether a given number is a strong number

A strong number is a number in which the sum of the factorials of the digits is equal to the number itself.

Example

  • 123!= 1! 2! 3!

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

145!=1! 4! 5!
  •                 120

                =145

In this example, 145 is a strong number because the sum of the factorials of the digits is equal to the number itself.

We use the following logic to determine whether

the given number is a strong number

:

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

The following is used to determine whether the given number is strong C program for strong numbers:

Online demonstration

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

Output

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

Run 1:
Enter a number : 145
145 is a strong number
Run 2:
Enter a number : 25
25 is not a strong number

The above is the detailed content of C program to determine whether a given number is a strong number. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Null pointer in CNext article:Null pointer in C