Thursday, 20 October 2016

7.c Write a C Program to compute the values of e^x value using Series expansion


#include<stdio.h>
#include<math.h>
#define PI 3.1415

double exp_x( int, int );

int fact( int );

int main()
{
    int choice, x, n;


                    printf( "e^x\nEnter x and n:\t" );
                    scanf( "%d %d", &x, &n );
                    printf( "e^%d(%d) = %f\n", x, n, exp_x( x, n ) );



}

double exp_x( int x, int n )
{
    int i = 1;
    float ex = 1;

    while ( i < n )
        {
            ex += ( double ) pow( x, i ) / fact( i );
            ++i;
        }

    return ex;
}



int fact( int num )
{
    int f = 0;

    if ( num == 1 )
        return 1;
    else
 f = num * fact( num - 1 );

    return f;
}

No comments:

Post a Comment