Tuesday 9 April 2013

c program to caculate Sum=1-x2/2!+ x4/4!- x6/6!+ x8/8!- x10/10!



 To calculate the sum. Sum=1-x2/2!+ x4/4!- x6/6!+ x8/8!- x10/10!
 Using non-recursion
 Main program:

Step 1: start
Step 2: declare x,i,n,s=0,c
Step 3: read x value
Step 4: for i=0 , n=0; i<=10; i=i+2, n++ goto step 5
Step 5: s=s+(pow(-1,n)*pow(x,i)/fact(i))
Step 6: print s value
Step 7: stop

 Sub program:

Step 1: while x!=0 goto Step 2
Step 2: y=y+x; x—
Step 3: return y
Step 4: return to main program

#include<stdio.h>
#include<math.h>
long fact(int);
void main()
 {
            int x,i,n;
              float s=0,c;
             clrscr();
             printf("\n enter the value of x\t");
            scanf("%d",&x);
/*perform the looping operation*/
              for(i=0,n=0;i<=10;i=i+2,n++)
            s=s+(pow(-1,n)*pow(x,i)/fact(i));
             printf("\n the result is %f",s);
             getch();
 }
/* calling sub program*/
long fact(int x)
{
            long int y=1;
            while(x!=0)
            {
                         y=y*x;
                         x--;
            }
             return y;
}

No comments: