编写程序求三角形的面积。已知三角形的三边a、b、c,则三角形的面积为
时间: 2023-07-24 15:54:55 浏览: 219
根据海伦公式,三角形的面积可以通过三条边a、b、c计算得出:
```
s = (a + b + c) / 2
area = sqrt(s * (s - a) * (s - b) * (s - c))
```
其中,s为半周长,sqrt为平方根函数。你可以使用这个公式来编写计算三角形面积的程序。以下是一个Python代码示例:
```
import math
a = float(input("请输入三角形的第一条边长:"))
b = float(input("请输入三角形的第二条边长:"))
c = float(input("请输入三角形的第三条边长:"))
s = (a + b + c) / 2
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
print("三角形的面积为:", area)
```
在程序中,我们使用了Python的math库中的sqrt函数来计算平方根。程序会提示用户输入三角形的三条边长,然后计算并输出三角形的面积。
相关问题
C语言程序设计已知三角形三边求三角形面积
可以使用海伦公式来计算三角形面积,公式为:S = sqrt(p * (p - a) * (p - b) * (p - c)),其中p为半周长,即p = (a + b + c) / 2,a、b、c为三角形的三条边长。在C语言中,可以使用以下代码实现:
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, p, s;
printf("请输入三角形的三条边长:\n");
scanf("%lf%lf%lf", &a, &b, &c);
p = (a + b + c) / 2;
s = sqrt(p * (p - a) * (p - b) * (p - c));
printf("三角形的面积为:%lf\n", s);
return 0;
}
用c语言 已知三角形是三边分别为 a b c 计算三角形的面积
你好,根据海伦公式,可以使用C语言计算三角形的面积,公式为:S=sqrt(p*(p-a)*(p-b)*(p-c)),其中p=(a+b+c)/2。具体代码如下:
```c
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, p, s;
printf("请输入三角形的三条边长:\n");
scanf("%lf%lf%lf", &a, &b, &c);
p = (a + b + c) / 2;
s = sqrt(p * (p - a) * (p - b) * (p - c));
printf("三角形的面积为:%lf\n", s);
return ;
}
```
希望能够帮到你。