152 Programming and Data Structures
Explanation Through the keyboard values of 'x ' and 'n ' are entered, where numerical constant
values are assigned to *x ' and ' n '. Throughout the series 'n ' decides how long the series should
continue. The series contains alternate + and - terms. The two fo r loops within i f are used for
calculating the factorials. The i f statement decides the positive or negative term depending upon
odd or even value of % c '.
The following are the programs based on series. The logic of the programs is simple for understanding.
6.37 Write a program to evaluate the series given in comment
/* x+x2/2!+xV4!+xV6!+...xn/n! *1
# in clud e < std io .h >
# in clu d e <math.h>
m ain()
{
in t f=>l , 1 , i , x , y ;
f lo a t sum;
c lr s c r O ;
printf ("Enter the value o fx & y
scanf ("%d %d",&cx,&cy);
sum = x;
for (i=2;i<=y,i+=2)
I
f= l;
fo r (l-l;l<=i;l++)
f= n ;
sum = sum + pow(x,i)lf;
I
printf ("rt Sum of Series : %f', sum);
getcheO;
I
QU 1 PUT:
Lnter the value of x & y : 4 4
Sum of Series :22.6666
6 38 Write a program to evaluate the series given in comment
I* l-l/l!+2/2!-3/3!...n/n! */
# include <stdio,h>
# include <conio.h>
# include <math.h>