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

   2024-10-01 3370
核心提示:要输出三角形的面积和周长,首先需要用户输入三角形的三条边长。然后根据输入的三边长计算三角形的面积和周长,最后将结果输出给

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

以下是一个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;}

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

 
举报打赏
 
更多>同类物流大全
推荐图文
推荐物流大全
点击排行

网站首页  |  关于我们  |  联系方式 | 网站留言    |  赣ICP备2021007278号