Write a Python Program to find Area of a Rectangle and Perimeter of a Rectangle with example. Before we step into the Program to find Area of a Rectangle example, Let see the definitions and formulas.
If we know the width and height then, we can calculate the area of a rectangle using below formula. Area = Width * Height
Perimeter is the distance around the edges. We can calculate perimeter of a rectangle using below formula: Perimeter = 2 * (Width + Height)
Python Program to find Area of a Rectangle and Perimeter of a Rectangle
This program for Area of a rectangle allows the user to enter width and height of the rectangle. Using those values, this python program will calculate the Area of a rectangle and perimeter of a rectangle.
width = float(input('Please Enter the Width of a Rectangle: '))
height = float(input('Please Enter the Height of a Rectangle: '))
# calculate the area
Area = width * height
# calculate the Perimeter
Perimeter = 2 * (width + height)
print("\n Area of a Rectangle is: %.2f" %Area)
print(" Perimeter of Rectangle is: %.2f" %Perimeter)

Following statements will allow the User to enter the Width and Height of a rectangle.
width = float(input('Please Enter the Width of a Rectangle: '))
height = float(input('Please Enter the Height of a Rectangle: '))
Next, we are calculating the area as per the formula.
Area = width * height
In the next Python line, We are calculating the Perimeter of a rectangle.
Perimeter = 2 * (width + height)
The following print statements will help us to print the Perimeter and Area of a rectangle. Also, refer to the Python Program to find the Area of a Circle.
print("\n Area of a Rectangle is: %.2f" %Area)
print(" Perimeter of Rectangle is: %.2f" %Perimeter)
Python Program to find Area of a Rectangle using functions
This area program allows the user to enter the width and height of a rectangle. We will pass those values to the function arguments to calculate the area of a rectangle.
def Area_of_a_Rectangle(width, height):
# calculate the area
Area = width * height
# calculate the Perimeter
Perimeter = 2 * (width + height)
print("\n Area of a Rectangle is: %.2f" %Area)
print(" Perimeter of Rectangle is: %.2f" %Perimeter)
Area_of_a_Rectangle(6, 4)
Within this area of the rectangle program, first, we defined the function with two arguments using the def keyword. This means the user will enter the width and height of a rectangle. Next, we are calculating the perimeter and Area of a rectangle as we described in our first example. For more, refer to the Python programs.
Area of a Rectangle is: 24.00
Perimeter of Rectangle is: 20.00
>>> Area_of_a_Rectangle(12, 9)
Area of a Rectangle is: 108.00
Perimeter of Rectangle is: 42.00
>>>
Also Read
Comments are closed.