In this C++ tutorial, you will learn how to find the cosine of an angle using cos() function of cmath, with syntax and examples.

C++ cos()

C++ cos() function returns cosine of an angle (in radians). Angle is passed as an argument to cos().

Syntax

The syntax of C++ cos() is

cos(x)

where

ParameterDescription
xAngle in radians.

Returns

Return value depends on the type of value passed for parameter x. The return value of cos(x) is

  • double if x is double.
  • float if x is float.
  • long double if x is long double.

The synopsis of cos() function is

double cos(double x);
float cos(float x);
long double cos(long double x);
double cos(T x); // for integral type argument values

cos() is a function of cmath library. Include cmath library in the program, if using cos().

ADVERTISEMENT

Example

In this example, we read angle value from user and find cosine of this angle using cos() function.

C++ Program

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

int main() {
    double x;
    cout << "Enter angle (in radians) : ";
    cin >> x;
    
    double result = cos(x);
    cout << "cos(" << x << ") : " << result << endl;
}

Output

Enter angle (in radians) : 1.2
cos(1.2) : 0.362358
Program ended with exit code: 0
Enter angle (in radians) : 0.5
cos(0.5) : 0.877583
Program ended with exit code: 0

Conclusion

In this C++ Tutorial, we learned the syntax of C++ cos(), and how to use this function to find the cosine of an angle, with the help of examples.