输入三角形三边长,按如下公式求三角形面积。的C语言
时间: 2023-06-10 14:06:26 浏览: 55
三角形面积公式为:s = (a+b+c)/2,其中a、b、c为三角形的三条边长,s为三角形的半周长,用公式S = sqrt(s(s-a)(s-b)(s-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("三角形面积为:%.2lf\n", area);
return 0;
}
```
在上述代码中,使用了double类型来存储三角形三边长、半周长和面积,以保证精度正确。scanf函数用来从标准输入中读取用户输入的三角形三边长,然后根据公式求出半周长和面积,最后使用printf函数输出结果。
相关问题
c语言。输入三角形的三边长求面积
可以使用海伦公式计算三角形面积,公式为:
s = (a + b + c) / 2
area = sqrt(s * (s-a) * (s-b) * (s-c))
其中,a、b、c 分别为三角形的三边长,s 为半周长,sqrt() 函数表示计算平方根。
以下是一个示例代码:
```c
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, s, area;
printf("请输入三角形的三边长:");
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语言输入三角形三边,求三角形面积
可以使用海伦公式来计算三角形的面积,公式为:S = sqrt(p * (p - a) * (p - b) * (p - c)),其中a、b、c为三角形的三条边,p为半周长,即p = (a + b + c) / 2。以下是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;
}
相关推荐
![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)