编写函数计算阶乘的和,在主函数中从键盘输入n,调用该函数计算1!+2!+3!+4!+5!+…+n! **输入提示信息格式要求:"Input n:" **输出格式要求:"1!+2!+…-+%d!=%d\n"
时间: 2023-11-27 07:48:16 浏览: 33
以下是C语言编写函数计算阶乘的和的代码,满足输入输出格式要求:
```c
#include <stdio.h>
int fact(int n);
int main() {
int i, n; long sum = 0;
printf("Input n:");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
sum += fact(i);
}
printf("1!+2!+...+%d!=%ld\n", n, sum);
return 0;
}
/* 函数功能:用迭代法计算整型变量n的阶乘 */
int fact(int n) {
int i;
long result = 1;
for (i = 2; i <= n; i++) {
result *= i;
}
return result;
}
```
相关问题
编写函数计算阶乘的和,在主函数中从键盘输入n,调用该函数计算1!+2!+3!+4!+5!+…+n!
以下是C语言编写函数计算阶乘的和的代码,可以实现从键盘输入n,调用该函数计算1!+2!+3!+4!+5!+…+n!的功能:
```c
#include<stdio.h>
int fact(int n);
int main() {
int i,n;
long sum=0;
printf("输入n(n>0)\n");
scanf("%d",&n);
for(i=1;i<=n;i++) {
sum+=fact(i);
}
printf("sum=%ld\n",sum);
}
/*函数功能:用迭代法计算整型变量n的阶乘*/
int fact(int n) {
int i;
long result=1;
for(i=2;i<=n;i++) {
result*=i;
}
return result;
}
```
编写函数计算阶乘的和,在主函数中从键盘输入n,调用该函数计算1!+2!+3!+4!+5!+…+n! **输入提示信息格式要求:"Input n:" **输出格式要求:"1!+2!+…+%d!=%d\n"c语言
以下是C语言编写函数计算阶乘的和的代码,可以实现从键盘输入n,调用该函数计算1!+2!+3!+4!+5!+…+n!:
```c
#include <stdio.h>
int fact(int n);
int main() {
int i, n;
long sum = 0;
printf("Input n:");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
sum += fact(i); }
printf("1!+2!+...+%d!=%ld\n", n, sum);
return 0;
}
/*函数功能:用迭代法计算整型变量n的阶乘*/
int fact(int n) {
int i;
long result = 1;
for (i = 2; i <= n; i++) {
result *= i;
}
return result;
}
```
相关推荐
![](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)
![](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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)