Home > Article > Backend Development > 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.
, 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!=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
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
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!