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():
    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()
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.

advertisement
advertisement
Runtime Test Cases
 
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.

Note: Join free Sanfoundry classes at Telegram or Youtube

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.