
- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C library - cos() function
The C library cos() function of type double accept the parameter as variable(x) that returns the cosine of a radian angle.
The cos is termed as cosine of an acute angle which define the right triangle.
Syntax
Following is the syntax of the C library function cos() −
double cos(double x)
Parameters
This function accepts only a single parameter −
x − This is the floating point value representing an angle expressed in radians.
Return Value
This function returns the cosine value of x.
Example 1
Following is the basic C library program to shows the usage of cos() function.
#include <stdio.h> #include <math.h> #define PI 3.14159265 int main () { double x, ret, val; x = 60.0; val = PI / 180.0; ret = cos( x*val ); printf("The cosine of %lf is %lf degrees\n", x, ret); x = 90.0; val = PI / 180.0; ret = cos( x*val ); printf("The cosine of %lf is %lf degrees\n", x, ret); return(0); }
Output
On execution of above code, we get the following result −
The cosine of 60.000000 is 0.500000 degrees The cosine of 90.000000 is 0.000000 degrees
Example 2
Below the program illustrates how to use cos() to find the value of cosine angle.
#include <stdio.h> #include <math.h> #define PI 3.141592654 int main() { double angle_degrees = 120.0; double angle_radians = (angle_degrees * PI) / 180.0; double result = cos(angle_radians); printf("Cosine of %.2lf degrees = %.2lf\n", angle_degrees, result); return 0; }
Output
After executing the code, we get the following result −
Cosine of 120.00 degrees = -0.50
Example 3
In this program, we set the ranges of degree betweeb 0(deg) to 180(deg) using the loop and find the angle result through cos().
#include <stdio.h> #include <math.h> #define PI 3.141592654 int main() { printf("Table of Cosine Values:\n"); for (int angle_degrees = 0; angle_degrees <= 180; angle_degrees += 10) { double angle_radians = (angle_degrees * PI) / 180.0; double result = cos(angle_radians); printf("Cos(%.2d degrees) = %.2lf\n", angle_degrees, result); } return 0; }
Output
The above code produces the following result −
Table of Cosine Values: Cos(1.00 degrees) = 1.00 Cos(0.98 degrees) = 0.98 Cos(0.94 degrees) = 0.94 Cos(0.87 degrees) = 0.87 Cos(0.77 degrees) = 0.77 Cos(0.64 degrees) = 0.64 Cos(0.50 degrees) = 0.50 Cos(0.34 degrees) = 0.34 Cos(0.17 degrees) = 0.17 Cos(-0.00 degrees) = -0.00 Cos(-0.17 degrees) = -0.17 Cos(-0.34 degrees) = -0.34 Cos(-0.50 degrees) = -0.50 Cos(-0.64 degrees) = -0.64 Cos(-0.77 degrees) = -0.77 Cos(-0.87 degrees) = -0.87 Cos(-0.94 degrees) = -0.94 Cos(-0.98 degrees) = -0.98 Cos(-1.00 degrees) = -1.00