c语言给出三角形的三边长,求三角形面积。
时间: 2023-09-12 15:02:41 浏览: 72
可以使用海伦公式求解三角形面积,公式如下:
s = (a + b + c) / 2
area = sqrt(s * (s - a) * (s - b) * (s - c))
其中,a、b、c 分别为三角形的三边长,s 为半周长,sqrt() 为求平方根的函数。
下面是使用 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", area);
return 0;
}
```
需要注意的是,如果输入的三边长不能构成三角形,那么面积计算是无意义的,因此需要进行三角形是否存在的判断。
相关问题
用C语言写已知三角形三边长 求三角形面积
可以使用海伦公式来计算已知三角形三边长时的面积。
海伦公式:设三角形三边长分别为a、b、c,p=(a+b+c)/2,则三角形面积S=sqrt(p(p-a)(p-b)(p-c))。
以下是一个使用C语言实现海伦公式计算三角形面积的例子:
```c
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, p, s;
printf("请输入三角形三边长:\n");
scanf("%f %f %f", &a, &b, &c);
p = (a + b + c) / 2;
s = sqrt(p * (p - a) * (p - b) * (p - c));
printf("三角形面积为:%f\n", s);
return 0;
}
```
在上面的例子中,使用了`scanf()`函数从控制台读取输入的三角形三边长,然后计算出三角形的面积并使用`printf()`函数输出结果。
使用c语言编写给出三角形三边求面积
以下是使用 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;
}
```
在程序中,我们使用了 `scanf` 函数获取用户输入的三角形的三条边长,然后根据海伦公式计算出三角形的半周长 `s`,最后使用 `sqrt` 函数计算出三角形的面积并输出。
相关推荐
![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)