In this python article, we would love to share with you algorithm to write a program to accept the length and breadth of a rectangle and print its area in python.
Area is 2-dimensional: it has a length and a width. Area is measured in square units such as square inches, square feet or square meters. To find the area of a rectangle, multiply the length by the width. The formula is: A = L * W where A is the area, L is the length, W is the width, and * means multiply.
The formula of the Area of Rectangle
When you know the width and height. So you can calculate the area of a rectangle using below formula:
Area = Width * Height
Perimeter is the distance around the edges. You can calculate perimeter of a rectangle using below formula:
Perimeter = 2 * (Width + Height)
Write a Program to find the Area of a Rectangle and Perimeter of a Rectangle in Python
Follow the below steps and Write a Program to find the Area of a Rectangle and Perimeter of a Rectangle in Python:
- Allow the User to input the Width and Height of a rectangle.
- Next, we are calculating the area as per the formula Area = width * height.
- In the next line, We are calculating the Perimeter of a rectangle Perimeter = 2 * (width + height).
- Print the Perimeter and Area of a rectangle.
# Write a Program to find the Area of a Rectangle and
# Perimeter of a Rectangle in Python
w = float(input('Please Enter the Width of a Rectangle: '))
h = float(input('Please Enter the Height of a Rectangle: '))
# calculate the area
Area = w * h
# calculate the Perimeter
Perimeter = 2 * (w + h)
print("\n Area of a Rectangle is: %.2f" %Area)
print(" Perimeter of Rectangle is: %.2f" %Perimeter)
After executing the python program, the output will be:
Please Enter the Width of a Rectangle: 10 Please Enter the Height of a Rectangle: 5 Area of a Rectangle is: 50.00 Perimeter of Rectangle is: 30.00
Python Program to Calculate the Area of a Rectangle using Function
Follow the below steps and Python Program to Calculate the Area of a Rectangle using Function:
- Defined the function with two arguments using def keyword.
- Calculating the perimeter and Area of a rectangle inside functions.
- Allow the User to input the Width and Height of a rectangle.
- Print the Perimeter and Area of a rectangle.
# Write a Program to find the Area of a Rectangle and
# Perimeter of a Rectangle in Python using functions
def AreaOfRectangle(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)
width = float(input('Please Enter the Width of a Rectangle: '))
height = float(input('Please Enter the Height of a Rectangle: '))
AreaOfRectangle(width, height)
After executing the python program, the output will be:
Please Enter the Width of a Rectangle: 15 Please Enter the Height of a Rectangle: 5 Area of a Rectangle is: 75.00 Perimeter of Rectangle is: 40.00