输入n,求1!+2!+3!+4!+…+n!C++
时间: 2023-11-20 21:06:18 浏览: 92
以下是C++求1!+2!+3!+4!+…+n!的代码:
```cpp
#include <iostream>
using namespace std;
int main() {
int n;
int sum = 0, team = 1;
cout << "请输入n的值:";
cin >> n;
if (n <= 0) {
cout << "n为正整数,请重新输入" << endl;
return 0;
}
for (int i = 1; i <= n; i++) {
team *= i;
sum += team;
}
cout << "该式的值为:" << sum << endl;
return 0;
}
```
相关问题
输入n,求1!+2!+3!+4!+…+n!单循环实现C++
以下是单循环实现输入n,求1!+2!+3!+4!+…+n!的C++代码:
```cpp
#include <iostream>
using namespace std;
int main() {
int n;
int sum = 0;
int m = 1;
cout << "请输入n的值:";
cin >> n;
for (int i = 1; i <= n; i++) {
m *= i;
sum += m;
}
cout << "1!+2!+3!+4!+…+n!的和为:" << sum << endl;
return 0;
}
```
输入n,求1!+2!+3!+4!+…+n!循环的嵌套C++
以下是输入n,求1!+2!+3!+4!+…+n!循环的嵌套C++代码:
```c++
#include <iostream>
using namespace std;
int main() {
int n;
int sum = 0;
int team = 1;
cout << "请输入n的值:";
cin >> n;
if (n <= 0) {
cout << "n为正整数,请重新输入" << endl;
return 0;
}
for (int i = 1; i <= n; i++) {
team = 1;
for (int j = 1; j <= i; j++) {
team *= j;
}
sum += team;
}
cout << "该式的值为:" << sum << endl;
return 0;
}
```
阅读全文