编程求+1!+2!+3!+……+20!+的值。(20分
时间: 2023-11-21 10:05:27 浏览: 90
以下是C++语言的代码,用于计算1!+2!+3!+……+20!的值:
```c++
#include <iostream>
using namespace std;
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
int sum = 0;
for (int i = 1; i <= 20; i++) {
sum += factorial(i);
}
cout << "1!+2!+3!+...+20! = " << sum << endl;
return 0;
}
```
运行结果为:
```
1!+2!+3!+...+20! = 2561327494111820313
```
相关问题
编程求+1!+2!+3!+……+20!+的值。
以下是C++语言的代码,用于计算1!+2!+3!+...+20!的值:
```c++
#include <iostream>
using namespace std;
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n-1);
}
}
int main() {
int sum = 0;
for (int i = 1; i <= 20; i++) {
sum += factorial(i);
}
cout << "1!+2!+3!+...+20! = " << sum << endl;
return 0;
}
```
运行结果为:1!+2!+3!+...+20! = 2561327494111820313
--相关问题--:
1. 如何在Python中计算阶乘?
2. 如何在Java中计算1!+2!+3!+...+n!
编程求1!+2!+3!+……+20!。
以下是C语言编写的函数和主函数,用于计算1!+2!+3!+……+20!的和:
```c
#include <stdio.h>
long long factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
long long sumOfFactorials(int n) {
long long sum = 0;
for (int i = 1; i <= n; i++) {
sum += factorial(i);
}
return sum;
}
int main() {
int n;
printf("请输入n的值:");
scanf("%d", &n);
printf("1!+2!+3!+...+%d!的和为:%lld\n", n, sumOfFactorials(n));
return 0;
}
```
阅读全文