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

  1. Ask the user for the input of the length (l) and breadth (b) of the rectangle using an input() function.
  2. Using the formula calculate the area of the rectangle (area = l * b).
  3. 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:

  1. how to take user input in python
  2. Python operators

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:

  1. how to take user input in python
  2. Python operators
  3. Python Functions

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
Author

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

Write A Comment