用C语言编程求:编程计算1!+2!+3!+……+n!的值(n<25,n的值由键盘输入)

求助呀!!!
2024-11-05 20:11:05
推荐回答(4个)
回答1:

n的值由键盘输入,做不到,除非用define,可以做到一改全改,因为n不可以做动态修改

回答2:

#include
#define N 2
int main()
{
int sum,i,j,a;
sum=0;
a=0;
for(i=1;i<=N;i++)
{
for(j=1,a=0;j<=i;j++) a=a+j;
sum=sum+a;
}
printf("sum=%d\n",sum);
return 0;
}

可以改变#define N 后面的数值来实现改变.

回答3:

这题很简单的吧!

我帮你写个:

#include
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

回答4:

#include
void
main()
{
long
i,n,num=1,result=0;
printf("Please
input
a
num:");
scanf("%ld",&n);
for(i=1;i<=n;i++)
{
num*=i;
result+=num;
}
printf("result=%ld",result);
getch();
}