Sum of cos series

Submitted by Karthikeyan on

To find sum of cos series for the given angle and n value


/*Program to find Cos value of given angle */
/*Formula cos x = 1 - x^2/2! + x^4/4! - x^6/6!+.....*/

#include	 
#include	 
#define  	PI		3.1416
main( )
{
	int   f, 
		   i, 
		   j, 
		   n, 
		   sign = 1, 
		   xdeg;
	float xrad, 
		   xt, 
		   cosx = 0.0;
	printf( "\n\t\t Sum of cos series" );
	printf( "\n\t\t*******************\n" );
	printf( "\nEnter the value of angle and n " );
	scanf( "%d ", &xdeg );
	scanf( "%d", &n );
	xrad = xdeg * PI / 180;
	for( i = 0; i <= n; i = i + 2 )
	{
		for( f = 1, j = 1; j <= i; j++ )
			f = f * j;
		xt	 = pow( xrad, i ) / ( float )f;
		cosx = cosx + sign * xt;
		sign = -sign;
	}
	printf( "\n The cosine value = %f", cosx );
	printf( "\n The use of library function cos in value = %f", cos( xrad ) );
	printf( "\n\n\t\t********************\n" );
	getch( );
}