Zhejiang University Edition "C Language Programming (3rd Edition)" Exercises 2-6

Exercise 2-6 Find the sum of the first N terms of the factorial sequence (15 points)

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

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

Output format: output integer results
in one line .

Sample input:

5

Sample output:

153

These days I'm a bit lazy (shy face), but it doesn't matter, I will correct it (hhhhhh) Trust me! ! !
Here comes the code :

#include"stdio.h"
//递归函数
double fact(int n)
{
    if(n >= 2)
    {
        return n*fact(n-1);
    }
    else if(n == 1)
    {
        return 1;
    }

}
int main()
{

    int n, i, s;
    scanf("%d", &n);
    for(i = 1;i <= n;i++)
    {
        s +=fact(i);
    }
    printf("%d", s);
    
    return 0;
}
Published 25 original articles · won 3 · views 240

Guess you like

Origin blog.csdn.net/oxygen_ls/article/details/105441854