C Program to find value of Cosine x up to given accuracy


January 26, 2023, Learn eTutorial
1661

What is a Cosine series?

The Trigonometric functions sine, cos and tan would relate to the angles of a triangle to its sides. Cosine which we call as cos x is the ratio of adjacent side length to Hypotenuse length.

cos x = adjacent side / hypotenuse

Normally, cos x values are diametrically opposed to sine x values, so cos 0 = 1, cos 90 = 0. Here in this C program, we are going to see how to calculate the value of cosine x upto given accuracy.

How can cos x value be calculated using C?

Here in this C program, we convert the value which we accept from the user in degrees to radians. We also get accuracy from the user's input. then we are using a do-while loop for calculating the value of the cos x using the equations. 

 'do-while'loop works till the condition is a match (acc <= fabs(cosval - cosx)) then we calculate the value of den as 2*n*(2*n-1), which uses the formula for finding the term as -term * x * x / den. Then we find the cos x using the formula cosx + term. Moreover, increment the n by one. Finally, we display the result using the printf statement. 

ALGORITHM

STEP 1: Import the Header libraries to use the built-in functions used in the C program. 

STEP 2: Declare and Define the variables used in the C program, including the value of cos x. 

STEP 3: Accept the value of the angle in Degree using the printf and scanf statements. 

STEP 4: Convert the value of Degree to Radians using the formula. 

STEP 5: Accept the accuracy from the user using printf and scanf.

STEP 6: Now assign the value for term = 1, cosx = term and n = 1.

STEP 7: Open a 'do while' loop till the condition meets using C syntax.

STEP 8: Inside the loop, calculate the value of 'den'

STEP 9: The value of cos x. is calculated using 'term' and increments the n by 1

STEP 10: Print the result of cos x..


This program uses the below concepts in C programming, We prefer you to go through these topics for a better understanding.

C Source Code

                                          #include <stdio.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 */
    printf("Enter the value of x (in degrees)\n"); /* accepts value of x in degree */
    scanf("%f", & x);
    x1 = x;
    x = x * (3.142 / 180.0); /* Converting user input value from degrees to radians*/
    cosval = cos(x);
    printf("Enter the accuracy 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

Enter the value of x (in degrees)
30

Enter the accuracy 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 accuracy for the result
0.0001
Sum of the cosine series       = 0.707031
Using Library function cos(45) = 0.707035