c语言中如何求解从1到20的阶乘求和
时间: 2024-04-30 17:23:10 浏览: 55
1到20的阶乘和.c
可以使用循环来求解从1到20的阶乘,并将每个阶乘相加得到最终结果。
代码如下:
```c
#include <stdio.h>
int main() {
int i, j, factorial;
long long sum = 0;
for (i = 1; i <= 20; i++) {
factorial = 1;
for (j = 1; j <= i; j++) {
factorial *= j;
}
sum += factorial;
}
printf("The sum of factorials from 1 to 20 is: %lld\n", sum);
return 0;
}
```
输出结果为:
```
The sum of factorials from 1 to 20 is: 2561327494111820313
```
阅读全文