梯形是一种四边形,至少有一对边彼此平行。梯形的面积和周长可以使用以下公式计算:
周长 = 所有边的总和
面积 = ½ x(平行边的长度之和)x 垂直线平行边之间的距离
代码逻辑 - 代码将使用 5 个变量作为梯形的所有边,并使用 1 个变量作为两个平行边之间的垂直距离。对于面积变量计算,我们将采用一个浮点变量,该变量将使用该值进行初始化。为了计算它,我们将使用公式“ ½ x(平行边长度之和)x 平行边之间的垂直距离”。对于周长计算,将为变量分配表达式“(所有边的总和)”。
下面的代码显示计算梯形面积和周长的程序,
示例 现场演示
#include <stdio.h>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 , 4distance between two parallel sides is 5perimeter of the trapezium is 14.0area of the trapezium is: 12.500
以上就是计算梯形的面积和周长的程序的详细内容。