用c++输出1!+2!+3!+……+20!的总值
时间: 2023-10-29 15:04:58 浏览: 29
#include <stdio.h>
int main() {
int i, j;
long long factorial = 1, sum = 0;
for (i = 1; i <= 20; i++) {
for (j = 1; j <= i; j++) {
factorial *= j;
}
sum += factorial;
factorial = 1;
}
printf("The sum of 1! to 20! is %lld", sum);
return 0;
}
相关问题
用c++输出1!+2!+3!+……+20!的值
以下是将1!到20!的值输出到屏幕的C代码:
```c
#include <stdio.h>
int main() {
int i;
long long factorial = 1;
for(i=1; i<=20; i++) {
factorial *= i;
printf("%d! = %lld\n", i, factorial);
}
return 0;
}
```
输出:
```
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 6227020800
14! = 87178291200
15! = 1307674368000
16! = 20922789888000
17! = 355687428096000
18! = 6402373705728000
19! = 121645100408832000
20! = 2432902008176640000
```
c++递归1!+2!+……+n!使用c++代码写出
以下是C++实现递归1!+2!+……+n!的代码:
```c++
#include <iostream>
using namespace std;
long long RJieCheng(int n) {
if (n == 1) {
return 1;
}
return n * RJieCheng(n - 1);
}
long long RSum(int n) {
if (n == 1) {
return 1;
}
return RJieCheng(n) + RSum(n - 1);
}
int main() {
int n;
cout << "请输入你要求的前n个数的阶乘的和: ";
cin >> n;
cout << "前n个数的阶乘的和为: " << RSum(n) << endl;
return 0;
}
```