Home >Backend Development >C#.Net Tutorial >How to find the area of ​​a triangle in c language

How to find the area of ​​a triangle in c language

王林
王林Original
2020-05-06 15:39:4336801browse

How to find the area of ​​a triangle in c language

1. Analysis

Input three side lengths, apply Helen's formula to calculate the area, and output.

You can first determine whether a triangle can be formed, that is, the sum of any two sides is greater than the third side, and then calculate if a triangle can be formed, which can increase the rigor.

2. Implementation code

#include <stdio.h>
#include <math.h>
int main()
{
 printf("请依次输入三个边长\n");
    double a,b,c,p,s;
    scanf("%lf%lf%lf",&a,&b,&c);
    if(a+b>c && a+c>b && b+c>a) //判断是否可以构成三角形。
    {
        p=(a+b+c)/2;//计算半周长
        s=sqrt(p*(p-a)*(p-b)*(p-c));//套用海伦公式,计算面积
        printf("面积为%lf\n", s);//输出结果
    }
    else printf("无法构成三角形\n");//输入不合法,提示。
    return 0;
}

Recommended tutorial: c language tutorial

The above is the detailed content of How to find the area of ​​a triangle in c language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn