c语言编程求 1!+2!+…+n!
时间: 2023-11-19 14:06:41 浏览: 90
以下是C语言编程求1!+2!+…+n!的例子:
```c
#include <stdio.h>
int fun(int n) {
int i, j, sum = 0, temp = 1;
for (i = 1; i <= n; i++) {
temp = 1;
for (j = 1; j <= i; j++) {
temp *= j;
}
sum += temp;
}
return sum;
}
int main() {
int n;
printf("请输入n的值:");
scanf("%d", &n);
printf("%d的阶乘之和为:%d\n", n, fun(n));
return 0;
}
```
相关问题
c语言编程求1!+2!+3!+……+n!
以下是C语言编程求1!+2!+3!+……+n!的函数fun以及在main函数中调用fun函数并输出结果的代码:
```c
#include <stdio.h>
int fun(int n) {
int i, j, sum = 0, temp = 1;
for (i = 1; i <= n; i++) {
temp = 1;
for (j = 1; j <= i; j++) {
temp *= j;
}
sum += temp;
}
return sum;
}
int main() {
int n, result;
printf("请输入n的值:");
scanf("%d", &n);
result = fun(n);
printf("%d的阶乘和为:%d\n", n, result);
return 0;
}
```
输入n的值后,程序会调用fun函数计算1!+2!+3!+……+n!的和,并输出结果。
编程c语言求1!+2!+3!++n!,要求n从键盘输入(n≥1)
以下是C语言求1!+2!+3!+...+n!的代码,其中n从键盘输入:
```c
#include <stdio.h>
int main() {
int n, i, j, factorial, sum = 0;
printf("请输入一个正整数n:");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
factorial = 1;
for (j = 1; j <= i; j++) {
factorial *= j;
}
sum += factorial;
}
printf("1!+2!+3!+...+n!的和为:%d\n", sum);
return 0;
}
```
阅读全文