c语言编写程序,计算并输出1!+2!+…+10!的值
时间: 2023-07-04 16:09:36 浏览: 117
可以使用循环和递归两种方式来实现,下面给出循环实现的示例代码:
```c
#include <stdio.h>
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 <= 10; i++) {
sum += factorial(i);
}
printf("1!+2!+...+10!=%d\n", sum);
return 0;
}
```
运行结果为:
```
1!+2!+...+10!=4037913
```
相关问题
编写C语言程序,计算并输出+1!+2!+3!+...+10!的值
以下是C语言程序,计算并输出+1!+2!+3!+...+10!的值:
```c
#include <stdio.h>
int main(void) {
int n = 10;
int i = 0;
int m = 1;
int sum = 0;
for (i = 1; i <= n; ++i) {
m = m * i;
sum = sum + m;
}
printf("sum = %d\n", sum);
return 0;
}
```
阅读全文