温馨提示×

c语言如何求三角形的面积和周长

小亿
87
2024-08-17 07:16:37
栏目: 编程语言

要求一个三角形的面积和周长,可以通过以下步骤来实现:

  1. 首先,根据三角形的三条边长,可以使用海伦公式计算三角形的面积。海伦公式如下:

    面积 = sqrt(s * (s - a) * (s - b) * (s - c))

    其中,s = (a + b + c) / 2,a、b、c分别为三角形的三条边长。

  2. 其次,可以直接将三条边长相加,即可得到三角形的周长。

下面是一个示例代码来计算三角形的面积和周长:

#include <stdio.h>
#include <math.h>

int main() {
    float a, b, c, s, area, perimeter;

    // 输入三角形的三条边长
    printf("Enter the three sides of the triangle: ");
    scanf("%f %f %f", &a, &b, &c);

    // 计算三角形的面积
    s = (a + b + c) / 2;
    area = sqrt(s * (s - a) * (s - b) * (s - c));

    // 计算三角形的周长
    perimeter = a + b + c;

    // 输出结果
    printf("Area of the triangle is: %.2f\n", area);
    printf("Perimeter of the triangle is: %.2f\n", perimeter);

    return 0;
}

通过以上代码,便可以计算出任意三角形的面秋和周长。

0