In this tutorial, you will learn about C math library function cos() that computes the cosine.

C math library function cos()

cos() is the standard C math library function that is defined in math library math.h.

#include<math.h>

Function prototype of C math library function cos()

double cos( double x );

where,

x = angle in radians ( floating point value )

Return Value of cos( )

This function returns the cosine value of x that ranges in the interval [ -1, 1 ].

The return type is double.

Example: C program to demonstrate the use of math library function cos()

/*Use of math library function cos()*/

#include<stdio.h>
#include<math.h>

#define PI 3.1415926

int main()
{
   double x ;

   x = 60;
   //calculation of cosine
   printf("\ncos( %.2lf ) = %.2lf \n", x, cos( (x * PI) / 180 ));

   x = 180;
   //calculation of cosine
   printf("\ncos( %.2lf ) = %.2lf \n", x, cos( (x * PI) / 180 ));

   return 0;
}

Output

c math function cos

 

Explanation

In the above program, we have calculated the cosine of x.

We have defined macro PI for representing the value of pi.
The input of cos( ) can be any value.