实验2-4-4 求阶乘序列前N项和(15 分)

实验2-4-4 求阶乘序列前N项和(15 分)

本题要求编写程序,计算序列 $1!+2!+3!+\cdots$ 的前N项之和。

输入格式:

输入在一行中给出一个不超过12的正整数N。

输出格式:

在一行中输出整数结果。

输入样例:

1
5

输出样例:

1
153
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>  
double fact(int n);
int main(void) {

double result,s=0;
int n,i;
scanf("%d",&n);
for(i=1;i<=n;i++){
result=fact(i);
s=s+result;
}
printf("%.0f",s);
return 0;
}
double fact(int n){
double total = 0;
if (n == 0){
total = 1;
}else{
total = n * fact(n - 1);
}
return total;
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×