Cosine or Cosx is defined as a trigonometric angle function in a right angle triangle, For a better understanding of this cosine C program example, we always recommend you to learn the basic topics of C programming listed below:
In this C program, we have to find the value of Cosine X up to the given accuracy. The Trigonometric functions would use to relate 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 A = adjacent side / 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 user's input. 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. after accepting the value of the angle in degree, we convert that value to radians. Then we accept the accuracy needed for the cos x. We assign the term as one and cos x as the term and then n as 1.
Open a 'do-while
'loop 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.
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 increment the n by 1.
STEP 10: Print the result of Cos x using the printf
built-in function in C programming.
#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() */
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