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

This question requires the preparation of program that calculates the sequence 1!+2!+3!+⋯of front Nand Paragraph.

Input format:

Enter a positive integer not exceeding 12 in one line N.

Output format:

Output integer results in one line.

Input sample:

5

Sample output:

153

Code:

# include <stdio.h>
# include <stdlib.h>

int main() {
    
    
    int N,sum = 0,i,j,value;
    scanf("%d",&N);
    for (i=1;i<=N;i++) {
    
    
        value = 1;
        for (j=1;j<=i;j++) {
    
    
            value *= j;
        }
        sum += value;
    }
    printf("%d",sum);
    return 0;
}

Submit screenshot:

Insert picture description here

Problem-solving ideas:

Pay attention to the factorial operation, it N!means 1*2*……N, so Nyou must use a two-layer loop to traverse when finding the sum of the antecedents!

Guess you like

Origin blog.csdn.net/weixin_43862765/article/details/114434239