c 求1!+2!+3!+4!+...20!
时间: 2024-01-02 17:21:30 浏览: 81
求1!+2!+…+20!的和
以下是C语言程序设计中求1!+2!+3!+4!+...+n!的代码:
```c
#include <stdio.h>
int main()
{
int n, i;
double sum = 0, factorial = 1;
printf("请输入一个正整数n:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
factorial *= i; // 计算i的阶乘
sum += factorial; // 将阶乘加入到总和中
}
printf("1!+2!+3!+...+%d!的值为:%lf", n, sum);
return 0;
}
```
运行结果:
```
请输入一个正整数n:20
1!+2!+3!+...+20!的值为:2561327494111820313.000000
```
阅读全文