# Python Program to Find the Area of a Rectangle Using Classes

«
»

This is a Python Program to find the area of a rectangle using classes.

Problem Description

The program takes the length and breadth from the user and finds the area of the rectangle using classes.

Problem Solution

1. Take the value of length and breadth from the user.
2. Create a class and using a constructor initialise values of that class.
3. Create a method called as area and return the area of the class.
4. Create an object for the class.
5. Using the object, call the method area() with the parameters as the length and breadth taken from the user.
6. Print the area.
7. Exit

Program/Source Code

Here is the source code of the Python Program to take the length and breadth from the user and find the area of the rectangle. The program output is also shown below.

```class rectangle():
self.length=length
def area(self):
a=int(input("Enter length of rectangle: "))
obj=rectangle(a,b)
print("Area of rectangle:",obj.area())

print()```
Program Explanation

1. User must enter the value of length and breadth.
2. A class called rectangle is created and the __init__() method is used to initialise values of that class.
3. A method called as area, returns self.length*self.breadth which is the area of the rectangle.
4. An object for the class is created.
5. Using the object, the method area() is called with the parameters as the length and breadth taken from the user.
6. The area is printed.

Note: Join free Sanfoundry classes at Telegram or Youtube
Runtime Test Cases
```
Case 1:
Enter length of rectangle: 4
Area of rectangle: 20

Case 2:
Enter length of rectangle: 15
Area of rectangle: 195```

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Take Python Programming Mock Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 