Friday, 23 March 2012

Program to calculate the sum of series.


Q: Write a Program to calculate the sum of series.


#include <stdio.h>
#include <conio.h>
long int factorial(int n);


void main()
{
 int n,i;
 float s,r;
 char c;
 repeat:
 printf("You have this series:- 1/1! + 2/2! + 3/3! + 4/4! ...");
 printf("\nTo which term you want its sum?  ");
 scanf("%d",&n);
 s=0;
 for (i=1;i<=n;i++)
 {   
 s=s+((float)i/(float)factorial(i)); 
 }
 printf("\n\nThe sum of %d terms is %f",n,s);
 fflush(stdin);
 printf ("\n\nDo you want to continue?(y/n):-  ");
 scanf("%c",&c);
 if (c=='y')
 goto repeat;
 getch();
}


long int factorial(int n)
 {
  if (n<=1)
return(1);
  else
n=n*factorial(n-1);
return(n);
 }


Output of the program seems to be like this






No comments:

Post a Comment