9. Write a program to generate sum of series 1!+2!+3!+--------------n!
SOURCE CODE:
Method 1...
#include <stdio.h>
int fact_sum(int n)
{ int i,c;
int sum=0,f=1;
for(i=1; i<=n;i++) {
f= f*i;
sum+=f;
printf("%d!",i);
if(n!=i){
printf("+");
}
}
return sum;
}
int main()
{ int n,c;
printf("enter the value of n\n");
scanf("%d",&n);
printf("=%d",fact_sum(n));
return 0;
}
Method 2...
#include <stdio.h>
int fact(int n)
{ if(n==1 || n==0)
return 1;
else
return n*fact(n-1);
}
int fact_sum(int n)
{ int i,c;
int sum=0;
for(i=1; i<=n;i++) {
c= fact(i);
sum+=c;
}
return sum;
}
int main()
{ int n,c;
printf("enter the value of n\n");
scanf("%d",&n);
printf("sum of factorials is %d",fact_sum(n));
return 0;
}
Comments
Post a Comment