0
0
CsharpProgramBeginner · 2 min read

C# Program to Find Area of Triangle with Example

You can find the area of a triangle in C# by using the formula area = 0.5 * base * height and printing the result, for example: double area = 0.5 * baseLength * height; Console.WriteLine(area);.
📋

Examples

Inputbase = 4, height = 3
OutputArea of triangle is 6
Inputbase = 10, height = 5
OutputArea of triangle is 25
Inputbase = 0, height = 7
OutputArea of triangle is 0
🧠

How to Think About It

To find the area of a triangle, you need to know its base length and height. The area is half the product of these two values. So, multiply the base by the height, then divide by 2 to get the area.
📐

Algorithm

1
Get the base length of the triangle from the user
2
Get the height of the triangle from the user
3
Calculate the area using the formula: area = 0.5 * base * height
4
Display the calculated area
💻

Code

csharp
using System;
class Program {
    static void Main() {
        Console.Write("Enter base of triangle: ");
        double baseLength = Convert.ToDouble(Console.ReadLine());
        Console.Write("Enter height of triangle: ");
        double height = Convert.ToDouble(Console.ReadLine());
        double area = 0.5 * baseLength * height;
        Console.WriteLine("Area of triangle is " + area);
    }
}
Output
Enter base of triangle: 4 Enter height of triangle: 3 Area of triangle is 6
🔍

Dry Run

Let's trace the example where base = 4 and height = 3 through the code

1

Input base

User enters 4, so baseLength = 4

2

Input height

User enters 3, so height = 3

3

Calculate area

area = 0.5 * 4 * 3 = 6

4

Print result

Output: Area of triangle is 6

StepVariableValue
1baseLength4
2height3
3area6
💡

Why This Works

Step 1: Getting inputs

We ask the user to enter the base and height, which are needed to calculate the area.

Step 2: Calculating area

We use the formula area = 0.5 * base * height because the area of a triangle is half the product of its base and height.

Step 3: Displaying output

We print the calculated area so the user can see the result.

🔄

Alternative Approaches

Using Heron's formula
csharp
using System;
class Program {
    static void Main() {
        Console.Write("Enter side a: ");
        double a = Convert.ToDouble(Console.ReadLine());
        Console.Write("Enter side b: ");
        double b = Convert.ToDouble(Console.ReadLine());
        Console.Write("Enter side c: ");
        double c = Convert.ToDouble(Console.ReadLine());
        double s = (a + b + c) / 2;
        double area = Math.Sqrt(s * (s - a) * (s - b) * (s - c));
        Console.WriteLine("Area of triangle is " + area);
    }
}
This method calculates area using all three sides, useful when height is unknown but sides are known; it is more complex but versatile.
Using a function to calculate area
csharp
using System;
class Program {
    static double TriangleArea(double b, double h) {
        return 0.5 * b * h;
    }
    static void Main() {
        Console.Write("Enter base: ");
        double baseLength = Convert.ToDouble(Console.ReadLine());
        Console.Write("Enter height: ");
        double height = Convert.ToDouble(Console.ReadLine());
        double area = TriangleArea(baseLength, height);
        Console.WriteLine("Area of triangle is " + area);
    }
}
Using a function improves code reuse and clarity, especially if area calculation is needed multiple times.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of operations (input, multiplication, output), so it runs in constant time.

Space Complexity

Only a few variables are used to store inputs and the result, so space usage is constant.

Which Approach is Fastest?

The direct formula method is fastest and simplest; Heron's formula is slower due to square root calculation but useful when height is unknown.

ApproachTimeSpaceBest For
Direct formula (0.5 * base * height)O(1)O(1)Known base and height
Heron's formulaO(1)O(1)Known all three sides, height unknown
Function-based calculationO(1)O(1)Code reuse and clarity
💡
Always convert user input to double before calculation to handle decimal values correctly.
⚠️
Forgetting to multiply by 0.5 or using integer division which can cause incorrect area calculation.