In this C++ tutorial, you will learn how to find the tangent of an angle using tan() function of cmath, with syntax and examples.
C++ tan()
C++ tan() returns tangent of angle given in radians.
Syntax
The syntax of C++ tan() is
</>
Copy
tan(x)
where
Parameter | Description |
---|---|
x | Angle in radians. |
Returns
The return value depends on the type of value passed for parameter x. The return value of tan(x) is
- double if x is double.
- float if x is float.
- long double if x is long double.
The synopsis of tan() function is
</>
Copy
double tan(double x);
float tan(float x);
long double tan(long double x);
double tan(T x); // for integral type argument values
tan() is a function of cmath library. Include cmath library in the program, if using tan().
Example
In this example, we read an angle value into x, from the user, and find the tangent of this angle using tan() function.
C++ Program
</>
Copy
#include <iostream>
#include<cmath>
using namespace std;
int main() {
float x;
cout << "Enter an angle (in radians) : ";
cin >> x;
double result = tan(x);
cout << "tan(" << x << ") : " << result << endl;
}
Output
Enter an angle (in radians) : 1.5
tan(1.5) : 14.1014
Program ended with exit code: 0
Enter an angle (in radians) : 0
tan(0) : 0
Program ended with exit code: 0
Conclusion
In this C++ Tutorial, we learned the syntax of C++ tan(), and how to use this function to find the tangent of an angle, with the help of examples.