Experiment 2-4-4 Find the sum of the first N terms of the factorial sequence (15 points)

This question requires writing a program to calculate the sum of the first N items of the sequence 1!+2!+3!+...

Input format:
Input a positive integer N not exceeding 12 in one line.

Output format:
output integer results in one line.

Input sample:
5
Output sample:
153
problem collection complete works portal

#include <stdio.h>
int fac(int n);
int main()
{
    
    
    int n, sum = 0;
    scanf("%d", &n);

    for (int i = 1; i <= n; i++)
        sum += fac(i);
    printf("%d", sum);
                
    return 0;
}
int fac(int n)
{
    
    
    int s = 1;
    for (int i = 1; i <= n; i++)
        s *= i;
    
    return s;
}

Guess you like

Origin blog.csdn.net/fjdep/article/details/112747147