编写程序,求1!+2!+3!+4!+...+20!
时间: 2024-04-15 20:22:45 浏览: 213
1+2!+3!+...+20!阶乘求和程序
以下是两种编程语言的例子,分别使用C语言和Python来求解1!+2!+3!+4!+...+20!的和:
1. 使用C语言编写程序[^1]:
```c
#include <stdio.h>
int main() {
int i, j;
double sum = 0, jc;
for (i = 1; i <= 20; i++) {
jc = 1;
for (j = 1; j <= i; j++) {
jc *= j;
}
sum += jc;
}
printf("%lf", sum);
return 0;
}
```
2. 使用Python编写程序:
```python
sum = 0
for i in range(1, 21):
factorial = 1
for j in range(1, i+1):
factorial *= j
sum += factorial
print(sum)
```
阅读全文