计算梯形的面积和周长的程序

计算梯形的面积和周长的程序

梯形是一种四边形,至少有一对边彼此平行。梯形的面积和周长可以使用以下公式计算:

周长 = 所有边的总和

面积 = ½ x(平行边的长度之和)x 垂直线平行边之间的距离

代码逻辑 - 代码将使用 5 个变量作为梯形的所有边,并使用 1 个变量作为两个平行边之间的垂直距离。对于面积变量计算,我们将采用一个浮点变量,该变量将使用该值进行初始化。为了计算它,我们将使用公式“ ½ x(平行边长度之和)x 平行边之间的垂直距离”。对于周长计算,将为变量分配表达式“(所有边的总和)”。

下面的代码显示计算梯形面积和周长的程序,

示例

 现场演示

#include
int main() {
int a = 2 , b = 3 , c = 5 , d = 4, h = 5;
float area, perimeter;
printf("The sides of trapezium are %d , %d , %d , %d

", a,b,c,d);
printf("Distance between two parallel sides is %d

", h);
perimeter = a+b+c+d;
area = 0.5 * (a + b) * h ;
printf("Perimeter of the trapezium is %.1f

", perimeter);
printf("Area of the trapezium is: %.3f", area);
return 0;
}

登录后复制

输出

The sides of trapezium are 2 , 3 , 5 , 4
Distance between two parallel sides is 5
Perimeter of the trapezium is 14.0
Area of the trapezium is: 12.500

登录后复制

以上就是计算梯形的面积和周长的程序的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!