Home > Backend Development > C++ > body text

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

PHPz
Release: 2023-09-09 13:25:02
forward
1324 people have browsed it

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);
Copy after login
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;
}
Copy after login

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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!