Wednesday 12 September 2012

To calculate the sum. Sum=1-x2/2!+ x4/4!- x6/6!+ x8/8!- x10/10!


Algorithm: 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
 
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;
}
Output:
1.Enter the value of x : 1
    The result is 0.540302
2 Enter the value of x: 2
   The result is -0.416155

Conclusion: The program is error free  
VIVA QUESATIONS:

1)      What is function ?
Ans: A function is a sub program  it returns a value.

2)      What is procedure ?
Ans: A procedure is a sub program it does not returns a value  
    
3)      What are the basic data types in C ?
Ans: int, char, float, double
4)      How to define preprocessor ?
Ans: By using the # symbal Ex: #include<stdio.h

1 comment: