Using c program calculate the value of cosinex up to the given accuracy




The trigonometric function are used to relate the angles of an triangle to its sides. cosine which we call as cosx is the ratio of adjacent side length to hypotenuse length.  

cosA = adjacent / hypotenuse normally cos x values come opposite as sine x  which means cos 0 = 1, cos 90 = 0.

Here In this C program we convert the value which we accept from the user in degrees to radians. we also get the accuracy from the users input.

Then in this c program we are using a do-while loop for calculating the value of the cos x using the equations in that do while loop.

===================================================================
#include <stdio.h>
 
#include <conio.h>
 
#include <math.h>              /* include header files for accessing libraries */
 
#include <stdlib.h>
 
void main()
 
{
 
  int  n, x1;
 
  float  acc, term, den, x, cosx=0, cosval;   /* declares cosx cosval etc variables */
 
  clrscr();
 
  printf("Enter the value of x (in degrees)\n");  /* accepts value of x in degree */
 
  scanf("%f",&x);
 
  x1 = x;
 
  /* Converting user input value from degrees to radians*/
 
  x = x*(3.142/180.0);
 
  cosval = cos(x);
 
  printf("Enter the accuary for the result\n");
 
  scanf("%f", &acc);
 
  term = 1;
 
  cosx = term;
 
  n = 1;
 
  do
 
  {
	 den = 2*n*(2*n-1);
 
	 term = -term * x * x / den;    /* doing calculations inside the do while loop as same as sinx, please refer the sinx program for details */
 
	 cosx = cosx + term;
 
	 n = n + 1;
 
  } while(acc <= fabs(cosval - cosx));
 
 
  printf("Sum of the cosine series       = %f\n", cosx);
 
  printf("Using Library function cos(%d) = %f\n", x1,cos(x));
 
}         /*End of main() */
==================================================================
OUTPUT of C program to find the value of cosx
==================================================================
Enter the value of x (in degrees)
30
Enter the accuary for the result
0.000001
Sum of the cosine series = 0.865991
Using Library function cos(30) = 0.865991

RUN 2
Enter the value of x (in degrees)
45
Enter the accuary for the result
0.0001
Sum of the cosine series = 0.707031
Using Library function cos(45) = 0.707035