C# program to find Area of Isoceles Triangle
Program
using System;
namespace AreaOfIsocelesTriangle
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the Length of Same Side:\t");
float a = float.Parse(Console.ReadLine());
Console.Write("Enter the Length of other Side:\t");
float b = float.Parse(Console.ReadLine());
//float root = (float)Math.Sqrt(3) / 4;
float area= (float)((b/4)*Math.Sqrt((4*a*a)-(b*b)));
Console.WriteLine("Area of Isosceles Triangle is:\t" + area);
}
}
}
This C# program calculates the area of an isosceles triangle, where two sides are of equal length, based on the user-provided values.
1. Namespace and Class Definition
- The program is inside the
AreaOfIsocelesTriangle
namespace. - The
Program
class contains theMain()
method where the logic is implemented.
2. Taking User Input
- The user is prompted to enter the length of the equal sides (
a
). - The user is also prompted to enter the base (
b
). - The inputs are read as strings and converted into
float
values usingfloat.Parse()
.
3. Calculating the Area
- The formula for the area of an isosceles triangle is: [ \text{Area} = \frac{b}{4} \times \sqrt{(4a^2 - b^2)} ]
- This formula is derived using Heron’s formula for a triangle where two sides are equal.
Math.Sqrt()
is used to calculate the square root.- The formula is applied to compute the
area
.
4. Displaying the Result
- The calculated area is printed using
Console.WriteLine()
.
Key Points:
- Uses
Math.Sqrt()
for precise square root calculations. - Accepts dynamic input from the user.
- Correct formula implementation for an isosceles triangle.
- Uses
float
to handle decimal values for accurate calculations.
Output
Enter the Length of Same Side: 13
Enter the Length of other Side: 16
Area of Isosceles Triangle is: 81.97561