C# Program to Find Area of Rectangle | Using Constructor
ADVERTISEMENTS
C# program to find area of rectangle.
In this article, you will learn how to make a C# program to find area of rectangle.
Example
Enter the length & width of the rectangle::
9
8
Area of the rectangle is: 72 units
You should have knowledge of the following topics in C# programming to understand this program:
- C# Oops Concepts
- C# System Library
Source Code
// C# Program to Find Area of Rectangle
using System;
public class W3CW {
static public void Main (string[] args) {
Console.WriteLine("Enter the length & width of the rectangle::\n");
float length=Convert.ToInt32(Console.ReadLine());
float width=Convert.ToInt32(Console.ReadLine());
// It will calculate the area of a rectangle
float area = length * width;
// It will print the final output
Console.WriteLine("Area of the rectangle is: "+area+" units");
}
}
Output
Enter the length & width of the rectangle::
9
8
Area of the rectangle is: 72 units
Explanation
In this given program, we have taken two inputs 9
& 8
length and width of the rectangle from the user via the system console. Then we applied the standard formula to these values to calculate the area.
Then it will return 72
units final output of the above program.
C# Program to Find Area of Rectangle using Constructor
// C# Program to Find Area of Rectangle using Constructor
using System;
public class Rect {
public float a, b;
// It's the default constructor of the `Rect` class
public Rect (float x, float y) {
a=x;
b=y;
}
// It will calculate the area of the rectangle
public float Area() {
return a * b;
}
}
public class W3CW {
// It's the driver function
static public void Main (string[] args) {
Console.WriteLine("Enter the length & width of the rectangle::\n");
float length=Convert.ToInt32(Console.ReadLine());
float width=Convert.ToInt32(Console.ReadLine());
// It will create the instance of the `Rect` class
Rect RectObj = new Rect(length, width);
// It will print the final output
Console.WriteLine("Area of the rectangle is: "+RectObj.Area()+" units");
}
}