1+1/1!+1/2!+…+1/20!求和c++
时间: 2023-09-09 11:06:01 浏览: 151
C++求和程序
5星 · 资源好评率100%
以下是使用C++计算1+1/1!+1/2!+...+1/20!的代码:
```cpp
#include <iostream>
using namespace std;
int main() {
double sum = 0.0, fact = 1.0;
for (int i = 1; i <= 20; i++) {
fact *= i;
sum += 1.0 / fact;
}
sum += 1.0; // 加上1
cout << "sum = " << sum << endl;
return 0;
}
```
输出结果为:sum = 2.71828
阅读全文