計算梯形面積和周長的程式


梯形是一種四邊形,至少有一對邊平行。可以使用以下公式求梯形的面積和周長:

周長 = 所有邊的和

面積 = ½ 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.