Thursday, 20 October 2016

7 a .Write a C Program to compute the values ofsin x value using Series expansion


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

double sin_x( int, int );

int fact( int );

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


                    printf( "sin(x)\nEnter x and n:\t" );
                    scanf( "%d %d", &x, &n );
                    printf( "\nsin(%d)(%d) = %f\n", x, n, sin_x( x, n ) );



}


double sin_x( int ang_deg, int no_of_terms )
{
    int term, j;
    double value = 0.0, ang_rad = 0.0;
    ang_rad = ( double ) ang_deg * PI / 180;

    for ( term = 1, j = 2;term < no_of_terms*2;term += 2, j++ )
        {
            value += ( double ) pow( -1, j ) * pow( ang_rad, term ) / fact( term );
        }

    return value;
}


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