温馨提示×

c语言如何输出三角形面积和周长

小亿
83
2024-08-12 08:24:37
栏目: 编程语言

要输出三角形的面积和周长,首先需要用户输入三角形的三条边长。然后根据输入的三边长计算三角形的面积和周长,最后将结果输出给用户。

以下是一个C语言程序示例,用于计算并输出三角形的面积和周长:

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

int main() {
    float side1, side2, side3;
    float perimeter, area, s;

    // 用户输入三角形的三条边长
    printf("Enter the length of side 1: ");
    scanf("%f", &side1);
    printf("Enter the length of side 2: ");
    scanf("%f", &side2);
    printf("Enter the length of side 3: ");
    scanf("%f", &side3);

    // 计算三角形的周长
    perimeter = side1 + side2 + side3;

    // 计算三角形的半周长
    s = perimeter / 2;

    // 计算三角形的面积
    area = sqrt(s * (s - side1) * (s - side2) * (s - side3));

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

    return 0;
}

通过上述程序,用户可以输入三角形的三条边长,程序会计算并输出该三角形的面积和周长。

0