Introduction
In this lab, you will learn how to compute the tangent of an angle in C programming. The lab covers the step-by-step process of reading an angle in radians, using the tan() function from the C math library to calculate the tangent value, and printing the result. By the end of this lab, you will have a solid understanding of performing trigonometric calculations in C.
The lab consists of two main steps: reading the angle in radians and using the tan() function to compute the tangent value. In the first step, you will learn how to prompt the user for an angle input in radians and store it in a variable. In the second step, you will apply the tan() function to the angle and display the resulting tangent value.
Read Angle in Radians
In this step, you will learn how to read an angle in radians for trigonometric calculations in C programming. Radians are the standard unit for angle measurements in mathematical computations.
First, create a new C file to implement the angle input:
cd ~/project
nano tangent_calculation.c
Now, add the following code to read an angle in radians:
#include <stdio.h>
#include <math.h>
int main() {
double angle_radians;
printf("Enter an angle in radians: ");
scanf("%lf", &angle_radians);
printf("Angle entered: %.2f radians\n", angle_radians);
return 0;
}
Let's break down the code:
- We include
<stdio.h>for input/output functions - We include
<math.h>to use mathematical functions liketan() double angle_radiansdeclares a variable to store the anglescanf()reads the angle input from the userprintf()displays the entered angle
Compile the program:
gcc tangent_calculation.c -o tangent_calculation -lm
Example of running the program:
./tangent_calculation
Example output:
Enter an angle in radians: 1.57
Angle entered: 1.57 radians
Use tan() Function
In this step, you will learn how to calculate the tangent of an angle using the tan() function from the C math library, building upon the previous step of reading an angle in radians.
Modify the existing tangent_calculation.c file to include tangent calculation:
nano ~/project/tangent_calculation.c
Update the code with the following implementation:
#include <stdio.h>
#include <math.h>
int main() {
double angle_radians;
printf("Enter an angle in radians: ");
scanf("%lf", &angle_radians);
printf("Angle entered: %.2f radians\n", angle_radians);
// Calculate tangent using tan() function
double tangent_value = tan(angle_radians);
printf("Tangent of the angle: %.4f\n", tangent_value);
return 0;
}
Key changes in the code:
tan(angle_radians)calculates the tangent of the input angle%.4fformat specifier displays the tangent with 4 decimal places- The result is stored in
tangent_valuevariable
Compile the updated program:
gcc tangent_calculation.c -o tangent_calculation -lm
Example of running the program:
./tangent_calculation
Example output:
Enter an angle in radians: 1.57
Angle entered: 1.57 radians
Tangent of the angle: 1255.7655
Note: The tangent of π/2 (1.57 radians) approaches infinity, which is why you see a very large number.
Print the Tangent Value
In this final step, you will learn how to format and print the tangent value with different output styles, enhancing the presentation of your trigonometric calculation.
Update the tangent_calculation.c file to include more comprehensive output:
nano ~/project/tangent_calculation.c
Modify the code to provide multiple output formats:
#include <stdio.h>
#include <math.h>
int main() {
double angle_radians;
printf("Enter an angle in radians: ");
scanf("%lf", &angle_radians);
// Calculate tangent using tan() function
double tangent_value = tan(angle_radians);
// Print tangent value with different formatting
printf("Angle: %.2f radians\n", angle_radians);
printf("Tangent (default): %f\n", tangent_value);
printf("Tangent (scientific notation): %e\n", tangent_value);
printf("Tangent (4 decimal places): %.4f\n", tangent_value);
return 0;
}
Compile the program:
gcc tangent_calculation.c -o tangent_calculation -lm
Example of running the program:
./tangent_calculation
Example output:
Enter an angle in radians: 0.5
Angle: 0.50 radians
Tangent (default): 0.546302
Tangent (scientific notation): 5.463020e-01
Tangent (4 decimal places): 0.5463
Key points about output formatting:
%ffor standard floating-point notation%efor scientific notation%.4ffor 4 decimal places precision- Different formats help in various scientific and engineering applications
Summary
In this lab, you learned how to read an angle in radians and calculate the tangent of that angle using the tan() function from the C math library. First, you created a C program to read an angle in radians from the user input. Then, you modified the program to calculate the tangent of the entered angle using the tan() function and display the result.
The key steps covered in this lab include: reading an angle in radians, using the tan() function to calculate the tangent, and printing the tangent value. These concepts are fundamental for performing trigonometric calculations in C programming.



