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

  

4
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

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#

1

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

formula for calculating the area of a triangle

1

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;
        }
    }
}
Condividi: