atan() Function Example in C Programming

In this source code example, we will see how to use the atan() function in C programming with an example.

atan() Function Overview

The atan() function in C is employed to determine the arctangent (or inverse tangent) of a given value. The result is expressed in radians and lies within the range \([-π/2, π/2]\). The function is part of the math.h library. It accepts one argument—the value for which the arctangent is to be calculated—and it can be any real number. 

Key Points: 

- It's essential to include the math.h header to utilize the atan() function. 

- The function's output will be in radians. If a degree measure is preferred, the result can be converted using: degree = radian × 180 / π

- To compile the program successfully, don't forget to link the math library using the -lm flag.

Source Code Example

#include <stdio.h>
#include <math.h>  // Necessary for atan()

int main() {
    double value, result_in_radians, result_in_degrees;

    // Prompt user for the value
    printf("Enter a value to compute its arctangent: ");
    scanf("%lf", &value);

    // Calculate the arctangent of the value
    result_in_radians = atan(value);

    // Convert the radian result to degrees
    result_in_degrees = result_in_radians * 180.0 / M_PI;

    // Display the results
    printf("Arctangent of %.2lf in radians is: %.2lf\n", value, result_in_radians);
    printf("Arctangent of %.2lf in degrees is: %.2lf\n", value, result_in_degrees);

    return 0;
}

Output

Enter a value to compute its arctangent: 1
Arctangent of 1.00 in radians is: 0.79
Arctangent of 1.00 in degrees is: 45.00

Explanation

1. We initiate the program by including the necessary header files: stdio.h for standard I/O functions and math.h to access the atan() function.

2. In the main() function, we solicit a value from the user.

3. The atan() function is invoked to compute the arctangent of the input value.

4. For ease of interpretation, the result is also converted to degrees.

5. Both the radian and degree outputs are presented to the user.

When compiling and running the provided C program, remember the importance of linking the math library:


Comments