Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to use cos() in C++

Sadia Zubair

In trigonometry, the cos function of a right-angled triangle is defined as the length of the adjacent side over the longest side, i.e., the hypotenuse. An image of this is shown below:

Figure 1

The cos function in C++ works precisely like the cosine function in trigonometry. The return value of the cos function is the cosine of an angle given in radian.

To use the cos function, the cmath header file needs to be included in the program. This is done in the following way:

#include <cmath>


The cos function takes one parameter in radians.

Return value

The cos function returns the value between -1 and 1.

The function can have the return value of:

  • float
  • double
  • long double

These types depend on the types of arguments the programmer gives to the functions. Below, Figure 2 shows the types of return types when different argument types are passed:

Figure 2


The code below shows how the cos function functions in C++:

#include <iostream>
#include <cmath>
using namespace std;

int main()
  double x = 0.5;
  // result is in double
  cout << "cos(x) = " << cos(x) << endl;
  return 0;




Sadia Zubair
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring