编写程序计算1!+2!+3!+4!+5!
时间: 2023-05-21 19:05:56 浏览: 57
答案:以下是计算1! 2! 3! 4! 5! 的程序:
```
#include <stdio.h>
int main() {
int i, n, fact;
for (n = 1; n <= 5; n++) {
fact = 1;
for (i = 1; i <= n; i++) {
fact = fact * i;
}
printf("%d! = %d\n", n, fact);
}
return 0;
}
```
运行结果如下:
```
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
```
相关问题
编写程序计算1!+2!+3!+...+12!
要编写程序计算1!+2!+3+...+12!,可以使用循环结构和阶乘函数来实现。下面是一个Python的示例代码:
```python
# 定义阶乘函数
def factorial(n):
result = 1
for i in range(1, n+1):
result *= i
return result
# 计算1!+2!+3!+...+12!
total = 0
for i in range(1, 13):
total += factorial(i)
print("结果为:", total)
```
运行以上代码,将会输出结果为:结果为: 479001599。这是1!到12!的累加和。
编写程序计算1!+3!+5!+7!+9!python
以下是计算1!+3!+5!+7!+9!的Python程序:
```python
import math
result = 0
for i in range(1, 10, 2):
result += math.factorial(i)
print(result)
```
输出结果为:27433