In order to convert degrees to radians, multiply the angle in degrees by $\pi$ and divide by $180\degree$:
$$45\degree = 45\degree \times \frac{\pi}{180\degree} \ =\frac{\pi}{4} $$
Take for example the code:
#include <stdio.h>
#include <math.h>
#define PI acos(-1.0)
void clear_input_buffer(){
int ch;
while((ch=getchar())!='\n'&&ch!=EOF);
}
int main(){
float angle_in_degrees;
int correct_inputs = 0;
do {
printf("Please enter an angle in degrees to convert to radians:\n");
correct_inputs = scanf("%f",&angle_in_degrees);
if (correct_inputs==0){
printf("Invalid input\n");
}
clear_input_buffer();
}while (correct_inputs==0);
printf("The angle in radians is %f\n",PI*angle_in_degrees/180.0);
return 0;
}
degrees-to-radians.c Copy
Conversely, we can convert from radians back to degrees by multiplying by $180\degree$ and dividing by $\pi$: $$\frac{\pi}{3}=\frac{\pi}{3}\times \frac{180\degree}{\pi}=60\degree$$
Challenge
Convert the following angles to radians:
- $45\degree$
- $360\degree$
- $-135\degree$
Convert the following angles to degrees:
- $\frac{\pi}{4}\ rad$
- $3\pi \ rad$
- $\frac{-\pi}{5}\ rad$
Rewrite the C code above to convert an angle in radians, provided by the user, back to degrees.