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


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

周长 = 所有边的和

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

代码逻辑 - 代码将使用 5 个变量作为梯形的所有边,以及一个变量表示两条平行边之间的垂直距离。对于面积变量计算,我们将使用一个初始化为该值的浮点型变量。为了计算它,我们将使用公式“½ 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 , 4
Distance between two parallel sides is 5
Perimeter of the trapezium is 14.0
Area of the trapezium is: 12.500

更新于: 2019年7月30日

480 次浏览

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.