编写C语言程序,输入一个整数n(n<=10),求sum=1!+2!+3!+.....+n!

2024-10-29 19:37:14
推荐回答(5个)
回答1:

#include

int main()

{int n,i,t=1,s=0;

scanf("%d",&n);

for(i=1;i<=n;i++)

{t*=i;

s+=t;

}

printf("%d",s);

return 0;

}

回答2:

#include"stdio.h"main(){ int n,i; long s=0,sum=1; scanf("%d",&n); for(i=1;i<=n;i++) { sum=sum*i; s=s+sum; } printf("%ld",s);}好了,就这样,不复杂

回答3:

递归
int n = 10;
long sum = 0;
for (int a = 1; a <= n; a++) {
if(
sum += getnum(a);
}

static int getnum(int i) {
if (i > 1) {
return getnum(i - 1) * i;
} else if (i == 1) {
return i;
}
return -1;
}

回答4:

#include
int main()
{
int i=0;
int n;
long temp=1;
long sum=0;
printf("please put a num n (n<=10)");
scanf("%d",&n)

for(i=1;i<=n;i++)
{
temp*=i;
sum+=temp;
}
printf("sum=%ld\n",sum);
return 0;
}
___________________

回答5:

#include
int f(int n)
{
if(n==1)return 1;
else return n*f(n-1);
}
void main()
{
int i,n,sum=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum+=f(i);
}

printf("%d\n",sum);
}