习题2-6 求阶乘序列前N项和 (15分)

本题要求编写程序,计算序列 1 的前N项之和。

输入格式:

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

输出格式:

在一行中输出整数结果。

输入样例:

5
 

输出样例:

153

#include<stdio.h>
int fact(int n);
int main(){
    int n,i;
    int sum=0;
    
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        sum+=fact(i);
    }
    printf("%d",sum);
}

int fact(int n){
    int j;
    int total=1;
    
    for(j=1;j<=n;j++){
        total*=j;
    }
    return total;
}

猜你喜欢

转载自www.cnblogs.com/Kimsohyun/p/12578879.html