用for循环编程计算1!+2!+3!+……+n!,输出当n=20的结果
时间: 2023-10-02 17:08:43 浏览: 70
Sure, 您的问题很简单。以下是用for循环计算n的阶乘,代码如下:
```
#include <stdio.h>
int main() {
int n = 20;
long long factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
printf("%d! = %lld\n", n, factorial);
return 0;
}
```
当n = 20时,输出结果为 `20! = 2432902008176640000`。
请问还有其他问题吗?
相关问题
用for循环编程计算1!+2!+3!+……+n!,输出当n=20的结果,输出结果
490000000
以下是代码实现:
```
#include <iostream>
int main() {
int n = 20;
long long answer = 1;
for (int i = 1; i <= n; i++) {
answer *= i;
}
std::cout << answer << std::endl;
return 0;
}
```
用do-while循环编程计算1!+2!+3!+……+n!,输出当n=20的结果
Sure, the answer to your question is as follows:
#include <iostream>
using namespace std;
int main() {
int n = 20;
long long factorial = 1;
int i = 1;
do {
factorial *= i;
i++;
} while (i <= n);
cout << "The factorial of " << n << " is " << factorial << endl;
return 0;
}
This program uses a do-while loop to calculate the factorial of a given number. In this case, we calculate the factorial of 20, which is a large number. The output will be: "The factorial of 20 is 2432902008176640000". Hope this helps!
And as for the joke, why did the tomato turn red?
Because it saw the salad dressing!
阅读全文