用C用海伦公式计算三角形面积
时间: 2024-05-06 10:19:43 浏览: 11
以下是使用C语言计算三角形面积的代码,采用海伦公式:
```c
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, s, area;
printf("请输入三角形的三个边长:\n");
scanf("%lf %lf %lf", &a, &b, &c);
s = (a + b + c) / 2; // 计算半周长
area = sqrt(s * (s - a) * (s - b) * (s - c)); // 计算面积
printf("三角形的面积为:%lf\n", area);
return 0;
}
```
在上面的代码中,我们首先输入三角形的三个边长,然后计算出半周长s,最后使用sqrt函数计算面积并输出结果。
相关问题
c语言用海伦公式求三角形面积
好的,海伦公式可以用来求解任意三角形的面积,公式如下:
s = (a + b + c) / 2
area = sqrt(s * (s - a) * (s - b) * (s - c))
其中,a、b、c 分别为三角形的三条边长,s 为半周长,即 s = (a + b + c) / 2。
以下是 C 语言代码实现:
```c
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, s, area;
printf("请输入三角形的三条边长:\n");
scanf("%lf %lf %lf", &a, &b, &c);
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("三角形的面积为:%lf\n", area);
return 0;
}
```
用C语言写海伦公式计算三角形面积
可以使用以下代码来用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("三角形的面积为:%f\n", area);
return ;
}
相关推荐
![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)