Print the Angle in Radians
In this step, you will learn how to convert and print the arcsin result in different angle representations, including radians and degrees.
Modify the previous program to include degree conversion:
cd ~/project
nano arcsin_angle_conversion.c
Write the following code to display angles in radians and degrees:
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
int main() {
double input;
printf("Enter a value between -1 and 1 for arcsin calculation: ");
scanf("%lf", &input);
// Validate input range
if (input < -1 || input > 1) {
printf("Error: Input must be between -1 and 1.\n");
return 1;
}
// Calculate arcsin
double radian_result = asin(input);
// Convert radians to degrees
double degree_result = radian_result * (180.0 / PI);
// Print results with formatting
printf("Input: %f\n", input);
printf("Arcsin result (radians): %f\n", radian_result);
printf("Arcsin result (degrees): %f\n", degree_result);
return 0;
}
Compile the program:
gcc arcsin_angle_conversion.c -o arcsin_angle_conversion -lm
Run the program and test:
./arcsin_angle_conversion
Example output:
Enter a value between -1 and 1 for arcsin calculation: 0.5
Input: 0.500000
Arcsin result (radians): 0.523599
Arcsin result (degrees): 30.000000
Key points about angle conversion:
- Radians are the default return type of
asin()
- Conversion to degrees: multiply by (180/π)
- Use
#define PI
for precise conversion
- Formatting helps readability of results