Compute Inverse Tangent (arctan) in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to compute the inverse tangent (arctan) function in C programming. The lab covers three main steps: reading a real number from user input, using the atan() function to calculate the inverse tangent, and printing the result in radians. By the end of this lab, you will have a solid understanding of how to perform trigonometric calculations using C.

The lab starts by teaching you how to read a real number from the user, which is essential for calculating the inverse tangent. Next, you will use the atan() function to compute the inverse tangent of the input number. Finally, you will print the result in radians, allowing you to interpret the output.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435149{{"`Compute Inverse Tangent (arctan) in C`"}} c/data_types -.-> lab-435149{{"`Compute Inverse Tangent (arctan) in C`"}} c/user_input -.-> lab-435149{{"`Compute Inverse Tangent (arctan) in C`"}} c/math_functions -.-> lab-435149{{"`Compute Inverse Tangent (arctan) in C`"}} end

Read Any Real Number

In this step, we will learn how to read a real number in C programming for calculating the inverse tangent (arctan) function.

First, let's create a new C source file to implement our program:

cd ~/project
nano read_number.c

Now, add the following code to the file:

#include <stdio.h>

int main() {
    double number;

    printf("Enter a real number: ");
    scanf("%lf", &number);

    printf("You entered: %f\n", number);

    return 0;
}

Let's break down the code:

  • double number; declares a variable to store a real number
  • scanf("%lf", &number); reads a double-precision floating-point number from user input
  • printf("You entered: %f\n", number); displays the entered number

Compile and run the program:

gcc read_number.c -o read_number
./read_number

Example output:

Enter a real number: 3.14
You entered: 3.140000

Use atan() Function

In this step, we will modify our previous program to use the atan() function to calculate the inverse tangent of the input number.

Update the read_number.c file with the following code:

cd ~/project
nano read_number.c

Add the following content:

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

int main() {
    double number, angle;

    printf("Enter a real number: ");
    scanf("%lf", &number);

    angle = atan(number);

    printf("Input number: %f\n", number);
    printf("Inverse tangent (arctan) in radians: %f\n", angle);

    return 0;
}

Key changes in the code:

  • Added #include <math.h> to use mathematical functions
  • Used atan() function to calculate the inverse tangent
  • Added a new angle variable to store the result
  • Printed both the input number and its inverse tangent

Compile the program with the math library:

gcc read_number.c -o read_number -lm
./read_number

Example output:

Enter a real number: 1
Input number: 1.000000
Inverse tangent (arctan) in radians: 0.785398

Print the Angle in Radians

In this step, we will enhance our program to provide more detailed output about the inverse tangent angle in radians.

Update the read_number.c file with the following code:

cd ~/project
nano read_number.c

Add the following content:

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

int main() {
    double number, angle;

    printf("Enter a real number: ");
    scanf("%lf", &number);

    angle = atan(number);

    printf("Input number: %f\n", number);
    printf("Inverse tangent (arctan) in radians: %f\n", angle);
    printf("Angle in degrees: %f\n", angle * 180.0 / M_PI);

    return 0;
}

Key changes in the code:

  • Added conversion of radians to degrees using angle * 180.0 / M_PI
  • Used M_PI constant from math.h for precise conversion
  • Added an additional print statement to show the angle in degrees

Compile the program:

gcc read_number.c -o read_number -lm
./read_number

Example output:

Enter a real number: 1
Input number: 1.000000
Inverse tangent (arctan) in radians: 0.785398
Angle in degrees: 45.000000

Summary

In this lab, we learned how to read a real number from user input and use the atan() function to calculate its inverse tangent (arctan) in radians. We started by creating a C program that prompts the user to enter a real number, and then we displayed the entered number. Next, we modified the program to use the atan() function to compute the inverse tangent of the input number and print the result in radians. The lab provided a step-by-step guide to implement these functionalities, ensuring a clear understanding of the process.

Other C Tutorials you may like