C语言 用函数调用实现 1!+2!+3!+4!+5!+6!
发布网友
发布时间:7小时前
我来回答
共2个回答
热心网友
时间:2024-10-20 07:48
#include<stdio.h>
int jiecheng(int n)
{
int t = 1, i;
for (i = 1; i <= n; i++)
{
t = t*i;
}
return t;
}
int main(void)
{
int i, sum = 0;
int n = 6;
for (i = 1; i <= n; i++)
sum = sum + jiecheng(i);
printf("1!+2!+3!+4!+5!+6!=%d\n", sum);
return 0;
}
热心网友
时间:2024-10-20 07:48
#include <stdio.h>
int factorial(int n) // 计算 1!+2!+...+n!
{
int i, temp = 1, sum = 0;
if(n <= 0)
return 1;
for(i = 1; i <= n; i++)
{
temp *= i;
sum += temp;
}
return sum;
}
int main()
{
int n = 0;
scanf("%d", &n);
printf("sum = %d\n",factorial(n));
return 0;
}