帮我用c语言写用海伦公式计算出三角形的面积并输出保留小数点后两位
时间: 2023-05-23 13:07:16 浏览: 97
A:
```c
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, s, area;
printf("请输入三角形的三边长:\n");
scanf("%f%f%f", &a, &b, &c);
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("三角形的面积为:%0.2f\n", area);
return 0;
}
```
使用海伦公式计算三角形面积,需要输入三角形的三边长,计算出半周长,然后代入公式进行计算,最后输出结果并保留小数点后两位。
相关问题
C语言实型数据处理:输入三角形三个边,利用海伦公式计算三角形的面积并输出,保留小数点后两位,
下面是一个用 C 语言实现的解法:
```c
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c;
float s, area;
printf("Enter the length of side a: ");
scanf("%f", &a);
printf("Enter the length of side b: ");
scanf("%f", &b);
printf("Enter the length of side c: ");
scanf("%f", &c);
// 计算半周长
s = (a + b + c) / 2.0;
// 计算面积
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("The area of the triangle is: %.2f\n", area);
return 0;
}
```
首先,我们定义了三个浮点型变量 a、b 和 c,分别表示三角形的三边长。接着,我们通过 `scanf` 函数从控制台读入三条边的长度。
然后,我们利用海伦公式,计算半周长 s,并用它计算出三角形的面积 area。最后,我们通过 `printf` 函数输出结果,保留小数点后两位。
需要注意的是,在计算平方根时,我们需要调用 `math.h` 头文件中的 `sqrt` 函数。
c语言,输入三角形三个边,利用海伦公式计算三角形的面积并输出,保留小数点后俩位
```c
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c;
double s, area;
scanf("%lf %lf %lf", &a, &b, &c);
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("%.2lf", area);
return 0;
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)