Introduction
In this lab, you will learn how to compute the cosine of an angle in C programming. The lab covers the following steps: reading the angle in radians, using the cos() function from the math.h library to calculate the cosine, and printing the resulting cosine value. By the end of this lab, you will have a solid understanding of how to perform trigonometric calculations using C.
The lab provides a step-by-step guide, starting with prompting the user to enter an angle in radians, then demonstrating how to use the cos() function to compute the cosine of the given angle, and finally, printing the calculated cosine value.
Read Angle in Radians
In this step, you will learn how to read an angle in radians for cosine calculation in C programming. We'll create a simple program that prompts the user to input an angle and prepares it for trigonometric calculations.
First, let's create a new C file for our cosine calculation program:
cd ~/project
nano cosine_calc.c
Now, add the following code to the file:
#include <stdio.h>
int main() {
double angle_radians;
// Prompt user to enter angle in radians
printf("Enter the angle in radians: ");
scanf("%lf", &angle_radians);
// Display the entered angle
printf("Angle entered: %.2f radians\n", angle_radians);
return 0;
}
Let's break down the code:
- We use
doubleto store the angle as a floating-point number for precise decimal representation printf()is used to prompt the user to enter an anglescanf()reads the user's input and stores it in theangle_radiansvariable- We then print back the entered angle to confirm the input
Compile the program:
gcc cosine_calc.c -o cosine_calc
Example output when running the program:
Enter the angle in radians: 3.14159
Angle entered: 3.14 radians
Use cos() Function
In this step, you will learn how to use the cos() function from the math.h library to calculate the cosine of an angle in C programming.
First, modify the previous cosine_calc.c file to include the cosine calculation:
cd ~/project
nano cosine_calc.c
Update the code with the following implementation:
#include <stdio.h>
#include <math.h>
int main() {
double angle_radians;
// Prompt user to enter angle in radians
printf("Enter the angle in radians: ");
scanf("%lf", &angle_radians);
// Calculate cosine of the angle
double cosine_value = cos(angle_radians);
// Display the angle and its cosine
printf("Angle: %.2f radians\n", angle_radians);
printf("Cosine of the angle: %.4f\n", cosine_value);
return 0;
}
Compile the program with the math library:
gcc cosine_calc.c -o cosine_calc -lm
Note the -lm flag, which links the math library containing trigonometric functions.
Example output when running the program:
Enter the angle in radians: 0
Angle: 0.00 radians
Cosine of the angle: 1.0000
Enter the angle in radians: 3.14159
Angle: 3.14 radians
Cosine of the angle: -1.0000
Key points about the cos() function:
- Imported from
math.hlibrary - Takes angle in radians as input
- Returns the cosine value as a double
- Works with standard mathematical angles
Print the Cosine Value
In this step, you will learn how to format and print cosine values with different precision and explore various output formatting techniques in C programming.
Let's modify the cosine_calc.c file to demonstrate multiple ways of printing cosine values:
cd ~/project
nano cosine_calc.c
Update the code with enhanced output formatting:
#include <stdio.h>
#include <math.h>
int main() {
double angle_radians;
// Prompt user to enter angle in radians
printf("Enter the angle in radians: ");
scanf("%lf", &angle_radians);
// Calculate cosine of the angle
double cosine_value = cos(angle_radians);
// Print cosine values with different formatting
printf("Cosine Calculation Results:\n");
printf("1. Default format: %f\n", cosine_value);
printf("2. Two decimal places: %.2f\n", cosine_value);
printf("3. Scientific notation: %e\n", cosine_value);
printf("4. Precise format: %.6f\n", cosine_value);
return 0;
}
Compile the program:
gcc cosine_calc.c -o cosine_calc -lm
Example output when running the program:
Enter the angle in radians: 1.5708
Cosine Calculation Results:
1. Default format: 0.000000
2. Two decimal places: 0.00
3. Scientific notation: 0.000000e+00
4. Precise format: 0.000000
Key points about printing cosine values:
- Use
%ffor floating-point numbers - Control decimal places with
.2f,.6fetc. - Use
%efor scientific notation - Experiment with different formatting specifiers
Summary
In this lab, you learned how to read an angle in radians and use the cos() function from the math.h library to calculate the cosine of the angle in C programming. You started by creating a program that prompts the user to enter an angle in radians and displays the entered value. Then, you modified the program to calculate the cosine of the angle using the cos() function and print the result.



