C++ tan()

This function is defined in <cmath> header file.


[Mathematics] tan x = tan(x) [In C++ Programming]

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

double tan(double x);
float tan(float x);
long double tan(long double x);
double tan (T x); // For integral type

tan() Parameters

The tan() function takes a single mandatory argument in radians (can be positive, negative, or 0).


tan() Return value

The tan() function returns the value in the range of [-∞, ∞].


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

#include <iostream>
#include <cmath>

using namespace std;

int main()
{ 
  long double x = 0.99999, result;
  result = tan(x);
  cout << "tan(x) = " << result << endl;
  
  double xDegrees = 60.0;
  // converting degree to radians and using tan() fucntion
  result = tan(xDegrees*3.14159/180);
  cout << "tan(x) = " << result << endl;

  return 0;
}

When you run the program, the output will be:

tan(x) = 1.55737
tan(x) = 1.73205

Example 2: tan() function with integral type

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

int main()
{
  long int x = 6;
  double result;

  result = tan(x);
  cout << "tan(x) = " << result;
  
  return 0;
}

When you run the program, the output will be:

tan(x) = -0.291006

Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges