编写一个c++程序,计算1!+2!+......+20!。
时间: 2023-11-17 16:00:23 浏览: 112
1+2!+3!+...+20!阶乘求和程序
以下是C++程序,用于计算1!+2!+......+20!:
```cpp
#include <iostream>
using namespace std;
int main() {
int n = 20;
long long sum = 0;
long long fac = 1;
for (int i = 1; i <= n; i++) {
fac *= i;
sum += fac;
}
cout << "1!+2!+...+20! = " << sum << endl;
return 0;
}
```
阅读全文