1
07/11/2024 2:07 pm
Emne starter
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!
2 Svar
1
07/11/2024 2:08 pm
- Declare 4 variables (a, b, h and area).
- Read the user input from the console: int.Parse(Console.ReadLine());
- Calculate the area of the trapezoid by the formula given in the picture above
- Print the result on the console (Console.WriteLine(area));).
1
07/11/2024 2:09 pm
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! 😉