Home >Backend Development >C#.Net Tutorial >Find factorial in c language
Purpose:
Input a number from the keyboard and find the factorial of this number, that is, n!.
Definition of factorial:
The so-called factorial of n means starting from 1 and multiplying it by a number that is 1 larger than the previous number, until it is multiplied to n. The formula is expressed as: 1×2×3 ×4×…×(n-2)×(n-1)×n=n!.
Algorithmic idea:
You can use loops to solve the problem. Let the loop variable be i, the initial value is 1, and i changes from 1 to n; multiply i and sum in turn, and add the product Assign to sum.
Example code:
#include <stdio.h> int main() { int i,n; double sum=1; scanf("%d",&n); for(i=1;i<=n;i++) sum=sum*i; printf("%d!=%lf",n,sum); printf("\n"); return 0; }
Run result:
Input 5, the corresponding factorial output is as follows:
5 5!=120.000000
Recommended tutorial: c language tutorial
The above is the detailed content of Find factorial in c language. For more information, please follow other related articles on the PHP Chinese website!