用函数实现求k !,并在主函数中调用求 sum=1!+2!+3!+……+n!。
展开全部
#include <stdio.h>
long factorial(long n)
{
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n-1);
}
void PrintResult(int n, long result)
{
int i = 0;
if (n == 0)
printf("%d! = ", 0);
else if (n == 1)
printf("%d! = ", 1);
else if (n == 2)
printf("%d!+%d! = ", 1, 2);
else
printf("%d!+%d!+...+%d! = ", 1, 2, n);
printf("%lu\n", result);
}
int main(void)
{
int n = 0, i = 0;
long sum = 0;
printf("Please input a number: \n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
sum += factorial(i);
}
PrintResult(n, sum);
return 0;
}
例子:
Please input a number:
20
1!+2!+...+20! = 268040729
Press any key to continue
long factorial(long n)
{
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n-1);
}
void PrintResult(int n, long result)
{
int i = 0;
if (n == 0)
printf("%d! = ", 0);
else if (n == 1)
printf("%d! = ", 1);
else if (n == 2)
printf("%d!+%d! = ", 1, 2);
else
printf("%d!+%d!+...+%d! = ", 1, 2, n);
printf("%lu\n", result);
}
int main(void)
{
int n = 0, i = 0;
long sum = 0;
printf("Please input a number: \n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
sum += factorial(i);
}
PrintResult(n, sum);
return 0;
}
例子:
Please input a number:
20
1!+2!+...+20! = 268040729
Press any key to continue
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询