M.Sc Python Programming Assignment 30

M.Sc Python Programming Assignments

Assignment 30

Question:

Write a Python class named Rectangle constructed by a length and width and a method which will compute the area and perimeter of a rectangle.

Code:

				
					class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def compute_area(self):
        return self.length * self.width

    def compute_perimeter(self):
        return 2 * (self.length + self.width)

# Example usage
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))

rectangle = Rectangle(length, width)

area = rectangle.compute_area()
perimeter = rectangle.compute_perimeter()

print("Area of the rectangle:", area)
print("Perimeter of the rectangle:", perimeter)

				
			

Output:

				
					Enter the length of the rectangle: 44
Enter the width of the rectangle: 33
Area of the rectangle: 1452.0
Perimeter of the rectangle: 154.0

				
			
Tech Amplifier Final Logo