Write a program to print the area and perimeter of a triangle having sides of 3, 4 and 5 units by creating a class named 'Triangle' with the constructor having the three sides as its parameters.(For C sharp)
internal class Program
{
class Triangle
{
public double AB { get; set; }
public double BC { get; set; }
public double CA { get; set; }
public Triangle(double ab, double bc, double ca)
{
AB = ab;
BC = bc;
CA = ca;
}
public string Perimeter()
{
return $"P = {AB}+{BC}+{CA} = {AB+BC+CA}";
}
public string Area()
{
double p = (AB + BC + CA) / 2;
return $"S = {Math.Sqrt(p * (p - AB) * (p - BC) * (p - CA))}";
}
}
static void Main()
{
Triangle triangle = new Triangle(3, 4, 5);
Console.WriteLine(triangle.Perimeter());
Console.WriteLine(triangle.Area());
Console.ReadKey();
}
}
Comments
Leave a comment