settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: How to calculate an area of a triangle in C# and Visual Basics

+5 votes

I need to create a program in Visual Basics (C#) which calculates the area of a triangle. Please help! thanks

asked in C# category by user ak47seo

3 Answers

+4 votes
 
Best answer

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:

calculating the area of a triangle in C#

answered by user matthew44
edited by user golearnweb
+4 votes

The formula for calculating is: Area of a triangle = 1/2(base x height)

formula for calculating the area of a triangle

answered by user matthew44
edited by user golearnweb
+2 votes

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;
        }
    }
}
answered by user ak47seo
edited by user golearnweb
...