A detailed example of the c language factorial summation problem
- 2020-06-19 11:10:14
- OfStack
Topic describes
S=1! + 2! + 3! +... + n! (n 50 or less)
Among them "! Represents the factorial, for example: 5! = 5 * 4 * 3 * 2 * 1.
Inputoutput format
Input format:
1 positive integer N.
Output format:
1 positive integer, S, represents the calculation result.
I/O sample
Enter the sample
3
The output sample
9
#include<stdio.h>
int fun(int n)
{
if(n==1||n==0)
{
return 1;
}
if(n>1)
{
return fun(n-1)*n;
}
}
int main()
{
int N,i,sum=0;
scanf("%d",&N);
for(i=1;i<=N;i++)
{
sum=sum+fun(i);
}
printf("%d\n",sum);
return 0;
}
or
#include<stdio.h>
int main()
{
int N,i,sum=0,t,h;
scanf("%d",&N);
for(i=1;i<=N;i++)
{
t=1;
h=i;
while(h)
{
t=t*h;
h--;
}
sum=sum+t;
}
printf("%d\n",sum);
return 0;
}
or
#include<stdio.h>
#include<String.h>
int main()
{
int N,i,sum=0,t=1;
int f,p=0;
scanf("%d",&N);
for(i=1;i<=N;i++)
{
t=i*t;
f=p+t;
p=f;
}
printf("%d\n",f);
return 0;
}
conclusion