📜  求梯形面积的程序

📅  最后修改于: 2021-10-23 09:10:09             🧑  作者: Mango

梯形的定义:
梯形是具有至少一对平行边的凸四边形。平行的边称为梯形的底,另外两条不平行的边称为腿。也可以有两对碱基。

上图中CD || AB,所以他们形成基地,另外两个边,即 AD 和 BC 形成腿。
梯形的面积可以使用这个简单的公式来计算:

Area=\frac{a+b}{2}h

a = 基数
b = 基数
h = 高度
例子 :

Input : base1 = 8, base2 = 10, height = 6
Output : Area is: 54.0

Input :base1 = 4, base2 = 20, height = 7
Output :Area is: 84.0

C++
// C++ program to calculate
// area of a trapezoid
#include
using namespace std;
 
// Function for the area
double Area(int b1, int b2,
                    int h)
{
    return ((b1 + b2) / 2) * h;
}
 
// Driver Code
int main()
{
    int base1 = 8, base2 = 10,
                height = 6;
    double area = Area(base1, base2,
                            height);
    cout << "Area is: " << area;
    return 0;
}
 
// This code is contributed by shivanisinghss2110


C
// CPP program to calculate
// area of a trapezoid
#include 
 
// Function for the area
double Area(int b1, int b2,
                    int h)
{
    return ((b1 + b2) / 2) * h;
}
 
// Driver Code
int main()
{
    int base1 = 8, base2 = 10,
                   height = 6;
    double area = Area(base1, base2,
                              height);
    printf("Area is: %.1lf", area);
    return 0;
}


Java
// Java program to calculate
// area of a trapezoid
import java.io.*;
 
class GFG
{
     
    // Function for the area
    static double Area(int b1,
                       int b2,
                       int h)
    {
        return ((b1 + b2) / 2) * h;
    }
 
    // Driver Code
    public static void main (String[] args)
    {
        int base1 = 8, base2 = 10,
                       height = 6;
        double area = Area(base1, base2,
                                  height);
        System.out.println("Area is: " + area);
    }
}


Python3
# Python program to calculate
# area of a trapezoid
 
# Function for the area
def Area(b1, b2, h):
    return ((b1 + b2) / 2) * h
 
# Driver Code
base1 = 8; base2 = 10; height = 6
area = Area(base1, base2, height)
print("Area is:", area)


C#
// C# program to calculate
// area of a trapezoid
using System;
 
class GFG
{
     
    // Function for the area
    static double Area(int b1,
                       int b2,
                       int h)
    {
        return ((b1 + b2) / 2) * h;
    }
 
    // Driver Code
    public static void Main ()
    {
        int base1 = 8, base2 = 10,
                       height = 6;
        double area = Area(base1, base2,
                                  height);
        Console.WriteLine("Area is: " + area);
    }
}
 
// This code is contributed by vt_m


PHP


Javascript


输出 :

Area is: 54.0