// 递归求阶乘和
#include <stdio.h>
double fact(int n); // 递归求阶乘
double sum(int n); // 递归求和
int main(void)
{
int n;
printf("请输入一个整数n(n>0且n<=10):");
scanf("%d", &n);
if (n == 1)
{
printf("1!=%.0f\n", sum(n));
}
else if (n == 2)
{
printf("1!+2!=%.0f\n", sum(n));
}
else if (n == 3)
{
printf("1!+2!+3!=%.0f\n", sum(n));
}
else
{
printf("1!+2!+...+%d!=%.0f\n", n, sum(n));
}
return 0;
}
double fact(int n)
{
double result;
if (n == 1)
{
result = 1;
}
else
{
result = n*fact(n-1);
}
return result;
}
double sum(int n)
{
double result;
if (n == 1)
{
result = fact(1);
}
else
{
result = fact(n)+sum(n-1);
}
return result;
}