Zhejiang University edition "C language programming experiment and exercise guidance (3rd edition)" topic collection
Experiment 5-7 Use a function to find the factorial sum of 1 to 10 (10 points)
This question requires the realization of a simple function for calculating the factorial of non-negative integers, so that the function can be used to calculate the value of 1!+2!+...+10!.
Function interface definition:
double fact( int n );
Where n is the parameter passed in by the user, and its value does not exceed 10. If n is a non-negative integer, the function must return the factorial of n.
Sample referee test procedure:
#include <stdio.h>
double fact( int n );
int main(void)
{
int i;
double sum;
sum = 0;
for(i = 1; i <= 10; i++)
sum = sum + fact(i);
printf("1!+2!+...+10! = %f\n", sum);
return 0;
}
/* 你的代码将被嵌在这里 */
Input sample:
There is no input for this question.
Output sample:
1!+2!+…+10! = 4037913.000000
Code implementation:
double fact( int n ){
int a;
int p=1;
for (a=1;a<=n;a++){
p*=a;
}
return p;
}
operation result: