find the area of a trapezoid in csharp

To find the area of a trapezoid in C#, use the formula:

main.cs
area = (b1 + b2) * h / 2;
26 chars
2 lines

Where b1 and b2 are the lengths of the parallel sides (base1 and base2 in the diagram below), and h is the height of the trapezoid (the perpendicular distance between the parallel sides).

Trapezoid Diagram

Here's an example C# method that calculates the area of a trapezoid given the lengths of its bases and height:

main.cs
public static double CalculateTrapezoidArea(double b1, double b2, double h)
{
    double area = (b1 + b2) * h / 2;
    return area;
}
134 chars
6 lines

You can call this method like this:

main.cs
double b1 = 5.0;
double b2 = 7.0;
double h = 4.0;
double area = CalculateTrapezoidArea(b1, b2, h);
Console.WriteLine("Area of trapezoid with bases {0} and {1} and height {2} is {3}", b1, b2, h, area);
201 chars
6 lines

Output:

main.cs
Area of trapezoid with bases 5 and 7 and height 4 is 24
56 chars
2 lines

gistlibby LogSnag