This problem required to achieve a non-negative integer factorial simple function calculations, and using the function evaluation 1! +2! +3! + ... + n! Value.
Function interface definition:
double fact( int n );
double factsum( int n );
Function fact
should return n
the factorial, it is recommended to achieve recursive. Function factsum
should return 1! + 2! + ... + n
! Values. Title ensure that the input and output in double precision.
Referee test program Example:
#include <stdio.h>
double fact( int n );
double factsum( int n );
int main()
{
int n;
scanf("%d",&n);
printf("fact(%d) = %.0f\n", n, fact(n));
printf("sum = %.0f\n", factsum(n));
return 0;
}
/* 你的代码将被嵌在这里 */
Sample Input 1:
10
Output Sample 1:
fact(10) = 3628800
sum = 4037913
Sample Input 2:
0
Output Sample 2:
fact(0) = 1
sum = 0
1 #include <stdio.h> 2 double fact(int n); 3 double factsum(int n); 4 5 int main() { 6 int n; 7 8 scanf("%d", &n); 9 printf("fact(%d) = %.0f\n", n, fact(n)); 10 printf("sum = %.0f\n", factsum(n)); 11 12 return 0; 13 } 14 double fact(int n) { 15 double res, sum; 16 if (n == 1 || n == 0) { 17 res = 1; 18 } else { 19 res = n * fact(n - 1); 20 } 21 return res; 22 } 23 double factsum(int n) { 24 double sum = 0; 25 if (n == 1) 26 sum = 1; 27 else if (n == 0) 28 sum = 2; 29 else { 30 sum = fact(n) + factsum(n - 1); 31 } 32 33 return sum; 34 }