settingsAccountsettings
Menusettings

Q: Finding the area of a trapezoid in C# with given sides a, b and the height h

+4 votes

I need to write a program in C# that finds the area of a trapezoid, given the base sides a, b and the height h.

Please help!

asked in C# category by user sam
edited by user golearnweb

2 Answers

+2 votes
 
Best answer

and the code itself:

using System;

class Trapezoid
{
    static void Main()
    {
        Console.WriteLine("Please write trapezoid's \"a\" base:");
        double a = double.Parse(Console.ReadLine());

        Console.WriteLine("Please write trapezoid's \"b\" base:");
        double b = double.Parse(Console.ReadLine());

        Console.WriteLine("Please write trapezoid's \"h\" height:");
        double h = double.Parse(Console.ReadLine());

        double area = ((a + b) / 2) * h;
        Console.WriteLine("The area of your trapezoid is: {0}",area);
    }
}

//////////////////////////////////////////////////////////////////////////////////

You must be very careful with the numeral data type you are using!

For example if you use int instead of double with:

a = 3

b = 4

h = 5

Instead of getting 17.5 for the area of this trapezoid you will get 15 (in the picture below!);

And as you know programming deleting is not like the usual one! ;-)

problem when using integers instead of double numeral data type

answered by user eiorgert
edited by user golearnweb
+1 vote

formula for calculating the area of trapezoid


  1. Declare 4 variables (a, b, h and area).
  2. Read the user input from the console: int.Parse(Console.ReadLine());
  3. Calculate the area of the trapezoid by the formula given in the picture above
  4. Print the result on the console (Console.WriteLine(area));).
answered by user eiorgert
edited by user golearnweb
...