c语言编程计算两角度加减
时间: 2023-08-09 10:09:33 浏览: 76
以下是使用C语言编写计算两个角度加减的示例代码:
```c
#include <stdio.h>
#include <math.h>
int main()
{
float angle1, angle2, sum, difference;
printf("Enter the first angle in degrees: ");
scanf("%f", &angle1);
printf("Enter the second angle in degrees: ");
scanf("%f", &angle2);
// Convert angles to radians
angle1 = angle1 * M_PI / 180;
angle2 = angle2 * M_PI / 180;
// Calculate sum and difference in radians
sum = angle1 + angle2;
difference = angle1 - angle2;
// Convert sum and difference back to degrees
sum = sum * 180 / M_PI;
difference = difference * 180 / M_PI;
printf("The sum of the two angles is: %.2f degrees\n", sum);
printf("The difference of the two angles is: %.2f degrees\n", difference);
return 0;
}
```
在这个示例中,我们首先提示用户输入两个角度,然后将它们转换为弧度。接下来,我们计算它们的和和差,然后将结果转换回度数并打印输出。注意,在进行角度转换时,我们使用了数学库中的`M_PI`常量,它表示圆周率$\pi$的值。