Friday 19 October 2012

Function035

Write a 'C' program to compute the following series: 1 - x + x ^ 2 / 2 - x ^ 3 / 6 + x ^ 4 / 24 + ..... + ( - 1 ) ^ nx ^ n / n ! where n and x is to be accepted by the user.

#include <stdio.h>
#include <conio.h>
#include <math.h>
float series ( int    n, float    x )
{                             
        int         i, sf = 1;
        float      s = 0, t;
        for ( i = 0; i <= n; i++ )
        {                                   
                 t = pow ( x, 2 ) / fact ( i );
                 s = s + sf * t;
                 sf = - sf;
        }
        return s;
}
                                    
int fact ( int    n )
{                                   
        int      f = 1;
        while ( n >= 1 )
        {                               
                  f = f * n;
                  n--;
        }
        return f;
}                            
                    
void main ( )
{                            
        int         n;
        float      f, x;
        clrscr ( );
                                  
        printf("\n\n\n\t\t Enter the value of n ( less than 8 )..:"); scanf("%d",&n);
        printf("\n\n\t\t Enter the value of x ..:"); scanf("%f",&x);
        f = series ( n, x );
        printf("\n\n\n\t\t Value = %f",f);
        getch ( );
}
Click here to contact us

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...