In this post, we will learn a python program to calculate the area of a rectangle with a very basic explanation and example.
So let us start learning from the very basics…
Formula For Area of Rectangle
The formula is: Area = l * b
- Where Area is area of Rectangle
- l is length of the Rectangle
- b is breadth of the Rectangle
Algorithm for Area of Rectangle
- Ask the user for the input of the length (
l
) and breadth (b
) of the rectangle using aninput()
function. - Using the formula calculate the area of the rectangle (
area = l * b
). - At the end, print the result (
print(area)
).
From the formula and algorithm, we understood how to implement a python program to calculate the area of the rectangle but before writing a program few programming concepts you have to know:
Source Code
l = float(input('Enter Length of the Rectangle: '))
b = float(input('Enter Breadth of the Rectangle: '))
area = l * b
print(f"Area of rectangle is {area:.2f}")
Output
Enter Length of the Rectangle: 4 Enter Breadth of the Rectangle: 5 Area of rectangle is 20.00 |
Now let us modify the above program and write it using the function.
Calculate the Area of Rectangle Using Function
But before writing this program you should know about:
Source Code
def Area_of_rectangle(length, breadth):
area = length * breadth
return area
l = float(input('Enter Length of the Rectangle: '))
b = float(input('Enter Breadth of the Rectangle: '))
print(f"Area of rectangle is {Area_of_rectangle(l,b):.2f}")
Output
Enter Length of the Rectangle: 8.1 Enter Breadth of the Rectangle: 4.7 Area of rectangle is 38.07 |