Print the Angle in Radians
In this step, you will learn how to convert and print the arccos angle in different formats, including radians, degrees, and a more readable representation.
Continue working in the same project directory:
cd ~/project
nano arccos_angle_print.c
Add the following code to demonstrate angle printing:
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
int main() {
double input;
printf("Enter a value between -1 and 1 for arccos calculation: ");
scanf("%lf", &input);
// Input validation
if (input < -1 || input > 1) {
printf("Error: Input must be between -1 and 1\n");
return 1;
}
// Calculate arccos
double angle_radians = acos(input);
// Convert radians to degrees
double angle_degrees = angle_radians * (180.0 / PI);
// Print angle in different formats
printf("Input value: %f\n", input);
printf("Angle in Radians: %f\n", angle_radians);
printf("Angle in Degrees: %f\n", angle_degrees);
// Print a descriptive representation
printf("Angle Description: %.2f radians (%.2f degrees)\n",
angle_radians, angle_degrees);
return 0;
}
Compile the program:
gcc arccos_angle_print.c -o arccos_angle_print -lm
Example outputs:
Enter a value between -1 and 1 for arccos calculation: 0.5
Input value: 0.500000
Angle in Radians: 1.047198
Angle in Degrees: 60.000000
Angle Description: 1.05 radians (60.00 degrees)
Enter a value between -1 and 1 for arccos calculation: -0.5
Input value: -0.500000
Angle in Radians: 2.094395
Angle in Degrees: 120.000000
Angle Description: 2.09 radians (120.00 degrees)
Key points:
- Used
PI
constant for degree conversion
- Converted radians to degrees using
angle_radians * (180.0 / PI)
- Demonstrated different formatting options for angle representation
- Used
%.2f
for two decimal place precision