This is a Python Program to find the area of a rectangle using classes.
The program takes the length and breadth from the user and finds the area of the rectangle using classes.
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
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(): def __init__(self,breadth,length): self.breadth=breadth self.length=length def area(self): return self.breadth*self.length a=int(input("Enter length of rectangle: ")) b=int(input("Enter breadth of rectangle: ")) obj=rectangle(a,b) print("Area of rectangle:",obj.area()) print()
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.
Case 1: Enter length of rectangle: 4 Enter breadth of rectangle: 5 Area of rectangle: 20 Case 2: Enter length of rectangle: 15 Enter breadth of rectangle: 13 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.
- Practice Programming MCQs
- Apply for Programming Internship
- Check Python Books
- Apply for Python Internship
- Check Information Technology Books