4
07/11/2024 2:28 pm
Argomento iniziale
I need to create a program in Visual Basics (C#) which calculates the area of a triangle. Please help! thanks
3 risposte
1
07/11/2024 2:29 pm
The CODE:
using System; using System.Text; class AreaTriangle { static void Main() { //Triangle Area = 1/2(base x height) Console.Write("Please write the \"b\" value of your triangle: "); decimal bSide = decimal.Parse(Console.ReadLine()); Console.Write("Please write the \"h\" value of your triangle: "); decimal hSide = decimal.Parse(Console.ReadLine()); decimal area = (bSide * hSide) / 2; Console.WriteLine("The area of your triangle is: {0}", area); } }
The reason for using decimal instead of int is because some values can be real numbers...
OUTPUT:
1
07/11/2024 2:30 pm
The formula for calculating is: Area of a triangle = 1/2(base x height)
1
07/11/2024 2:30 pm
This code I think is more accurate:
using System; namespace area { class Program { static void Main(string[] args) { double b; double h; Console.WriteLine("Enter your base length: "); b = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Enter the height: "); h = Convert.ToDouble(Console.ReadLine()); double area = Program.calcArea(b, h); Console.WriteLine(area); Console.ReadLine(); } public static double calcArea(double b, double h) { return 0.5 * b * h; } } }