C语言编写程序求1!+2!+….n!
时间: 2023-08-04 22:05:54 浏览: 138
您好!您可以使用C语言编写一个函数来计算1!+2!+....+n!,具体实现可以参考以下代码:
```c
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int n, sum = 0;
printf("请输入n的值:");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum += factorial(i);
}
printf("1!+2!+...+%d!的值为:%d\n", n, sum);
return 0;
}
```
这段代码中,factorial函数用来计算n的阶乘,主函数中使用for循环来计算1!+2!+....+n!的值,并输出结果。
相关问题
用C语言编写程序求1!+2!+....+10!
以下是用C语言编写程序求1!+2!+....+10!的代码:
```c
#include <stdio.h>
int main() {
int i, j;
double sum = 0, fac = 1;
for (i = 1; i <= 10; i++) {
fac *= i;
sum += fac;
}
printf("1!+2!+....+10!=%.0f\n", sum);
return 0;
}
```
编写程序,求1!+2!+3!+...+10!,验证1!+...+10!用c语言
以下是用C语言编写程序,求1!+2!+3!+...+10!的代码:
```c
#include <stdio.h>
int main() {
int n = 10;
int sum = 0;
int sum1 = 1;
for(int i = 1; i <= n; i++) {
sum1 *= i;
sum += sum1;
}
printf("%d\n", sum);
return 0;
}
```
运行结果为:4037913
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)