This problem requires a simple calculation function factorial non-negative integer, so that this function can be used to generate a factorial table, outputs 0! ~ N! Value.
Function interface definition:
double fact( int n ); |
---|
Wherein n is a parameter passed in user. If a non-negative integer n, this function shall return the factorial of n.
Referee test program Example:
#include <stdio.h>
double fact( int n );
int main()
{
int i, n;
double result;
scanf("%d", &n);
for(i = 0; i <= n; i++){
result = fact(i);
printf("%d! = %.0f\n", i, result);
}
return 0;
}
/* 你的代码将被嵌在这里 */
Sample input:
3 |
---|
Sample Output:
! 1 = 0
! = 1 1
! 2 = 2
! 3 = 6
double fact(int n)
{
if(n==0)
return 1;
else if(n==1)
return 1;
else
return n*fact(n-1);
}