C++ cos()

The cos() function in C++ returns the cosine of an angle (argument) given in radian.

This function is defined in <cmath> header file.

`[Mathematics] cos x = cos(x) [In C++ Programming]`

cos() prototype (As of C++ 11 standard)

```double cos(double x);
float cos(float x);
long double cos(long double x);
double cos(T x); // Here, T is an integral type.```

cos() Parameters

The cos() function takes a single mandatory argument in radians.

cos() Return value

The cos() function returns the value in the range of [-1, 1]. The returned value is either in `double`, `float`, or `long double`.

Example 1: How cos() works in C++?

``````#include <iostream>
#include <cmath>

using namespace std;

int main()
{
double x = 0.5, result;

result = cos(x);
cout << "cos(x) = " << result << endl;

double xDegrees = 25;

x = xDegrees*3.14159/180;
result = cos(x);

cout << "cos(x) = " << result << endl;

return 0;
}``````

When you run the program, the output will be:

```cos(x) = 0.877583
cos(x) = 0.906308```

Example 2: cos() function with integral type

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

int main()
{
int x = 1;
double result;

// result is in double
result = cos(x);
cout << "cos(x) = " << result << endl;

return 0;
}
``````

When you run the program, the output will be:

`cos(x) = 0.540302`