Write a program to print the area of two rectangles having sides (4,5) and (5,8) respectively by creating a class named 'Rectangle' with a function named 'Area' which returns the area. Length and breadth are passed as parameters to its constructor.(For C sharp)
using System;
namespace ConsoleApp1
{
class Program
{
public class Rectangle
{
double l, b;
public Rectangle(double length, double breadth)
{
l = length;
b = breadth;
}
public double Area()
{
return l * b;
}
}
static void Main(string[] args)
{
Rectangle rect1 = new Rectangle(4, 5);
Rectangle rect2 = new Rectangle(5, 8);
Console.WriteLine("Area of the first rectangle: {0}", rect1.Area());
Console.WriteLine("Area of the second rectangle: {0}", rect2.Area());
}
}
}
Comments
Leave a comment