Open In App

tan() Function in C

Last Updated : 26 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

tan() function in C programming is provided by the math.h header file and it is used to calculate the tangent of an angle x where x is represented in radians. This function returns the tangent of a given angle value in radians and it only works for right-angled triangles.

Syntax of tan()

double tan(double x);

Parameters of tan()

The tan(x) function takes a single parameter x.

  • x: is a double value representing the angle in radians for which the tangent value is to be computed.

Return value of tan()

The tan(x) function returns the tangent of the specified angle x which is passed in the form of radian.

Example of tan() Function in C

Input:
double radian = π/4 // value of π = 3.141

Output:
The tangent of 0.785 radians (45 degrees) is 1.000

The below program demonstrates how we can calculate the tangent for 30, 45, 60 and 90 degrees using the tan(x) function in C.

C
// C program to calculate the tangent of 30, 45, 60 and 90
// degree in C

#include <math.h>
#include <stdio.h>

int main()
{
    // 30 degrees in radians
    double a = M_PI / 6;
    // 45 degrees in radians
    double b = M_PI / 4;
    // 60 degrees in radians
    double c = M_PI / 3;
    // 90 degrees in radians
    double d = M_PI / 2;
    // Variable to store the results
    double res;

    res = tan(a);
    printf("The tangent of %.3lf radians (30 degrees) is "
           "%.3lf\n",
           a, res);

    res = tan(b);
    printf("The tangent of %.3lf radians (45 degrees) is "
           "%.3lf\n",
           b, res);

    res = tan(c);
    printf("The tangent of %.3lf radians (60 degrees) is "
           "%.3lf\n",
           c, res);

    res = tan(d);
    printf("The tangent of %.3lf radians (90 degrees) is "
           "%.3lf\n",
           d, res);

    return 0;
}


Output

The tangent of 0.524 radians (30 degrees) is 0.577
The tangent of 0.785 radians (45 degrees) is 1.000
The tangent of 1.047 radians (60 degrees) is 1.732
The tangent of 1.571 radians (90 degrees) is 16331239353195370.000

Time Complexity: O(1)
Auxiliary Space: O(1)

Explanation: In the above program M_PI is a macro constant defined in the <math.h> header which represents the mathematical constant π (pi) which is approximately equal to 3.14159265358979323846


Next Article

Similar Reads