C++ Program to find the tangent of given radian value


The right-angled triangle is the subject of the trigonometry concept known as the Tangent. The ratio between the angle's opposing leg and its adjacent leg when it is thought of as a right triangle is the trigonometric function for an acute angle. To calculate the tangent, we need to know the angle between the hypotenuse and the adjacent edge. Let the angle is 𝜃. The tan$(\theta𝜃)$ is like below:

$$\mathrm{tan(\theta)\:=\:\frac{opposite}{adjacent}}$$

In this article, we shall discuss the techniques to get the value of tan$(\theta𝜃)$ in C++ when the angle is given in the radian unit.

The tan() function

To compute the tan$(\theta𝜃)$ The tan() method from the cmath package must be used. This function outputs the value instantly after accepting an angle in radians. Here, the simple syntax is used −

Syntax

#include < cmath >
tan( <angle in radian> )

Algorithm

  • Take angle x in radian as input.
  • Use tan( x ) to calculate the tan (x).
  • Return result.

Example

#include <iostream> #include <cmath> using namespace std; float solve( float x ) { float answer; answer = tan( x ); return answer; } int main() { cout << "The value of tan( 1.054 ) is: " << solve( 1.054 ) << endl; cout << "The value of tan( 3.14159 ) is: " << solve( 3.14159 ) << endl; cout << "The value of tan with an angle of 90 degrees is: " << solve( 90 * 3.14159 / 180 ) << endl; cout << "The value of tan with an angle of 45 degrees is: " << solve( 45 * 3.14159 / 180 ) << endl; }

Output

The value of tan( 1.054 ) is: 1.75959
The value of tan( 3.14159 ) is: -2.53518e-06
The value of tan with an angle of 90 degrees is: 788898
The value of tan with an angle of 45 degrees is: 0.999999

The first two input values in this example are in radians, whereas the latter two input values are angles in degrees that have been converted to radians using the formula below −

$$\mathrm{\theta_{rad}\:=\:\theta_{deg}\:\times\:\frac{\pi}{180}}$$

For tan(90) the value is a large integer number which signifies the infinity since tan(90°) is undefined.

Conclusion

The tan() function in C++ can be used to obtain the tan value in radians for the given angle. Although this function is a part of the standard library, to use it, our C++ code must include the cmath header file. The C90 version of C++ featured a double return type, whereas later versions had overloaded methods for float and long double as well as improved generic (template) usage for integral types. Various parameters in either radians or degrees have been used with this function throughout the article; however, for degrees, the values are converted into radians using the formula given above.

Updated on: 17-Oct-2022

382 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements