0
0
PythonProgramBeginner · 2 min read

Python Program to Calculate Area of Rectangle

You can calculate the area of a rectangle in Python by multiplying its length and width using area = length * width.
📋

Examples

Inputlength = 5, width = 3
OutputArea of rectangle is 15
Inputlength = 10, width = 7
OutputArea of rectangle is 70
Inputlength = 0, width = 4
OutputArea of rectangle is 0
🧠

How to Think About It

To find the area of a rectangle, you multiply its length by its width. Think of it like counting how many square units fit inside the shape. So, you get the length and width values, then multiply them to get the area.
📐

Algorithm

1
Get the length of the rectangle from the user
2
Get the width of the rectangle from the user
3
Multiply length by width to find the area
4
Display the calculated area
💻

Code

python
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
area = length * width
print("Area of rectangle is", area)
Output
Enter the length of the rectangle: 5 Enter the width of the rectangle: 3 Area of rectangle is 15.0
🔍

Dry Run

Let's trace the example where length = 5 and width = 3 through the code

1

Input length

User enters 5, so length = 5.0

2

Input width

User enters 3, so width = 3.0

3

Calculate area

area = length * width = 5.0 * 3.0 = 15.0

4

Print result

Output: Area of rectangle is 15.0

StepVariableValue
1length5.0
2width3.0
3area15.0
💡

Why This Works

Step 1: Getting inputs

We use input() to get length and width from the user and convert them to numbers with float() so we can do math.

Step 2: Calculating area

The area is found by multiplying length and width using *, which gives the total space inside the rectangle.

Step 3: Showing output

We print the result with print() so the user sees the area value clearly.

🔄

Alternative Approaches

Using a function
python
def rectangle_area(length, width):
    return length * width

l = float(input("Length: "))
w = float(input("Width: "))
print("Area of rectangle is", rectangle_area(l, w))
This approach organizes code into a reusable function, making it easier to calculate area multiple times.
Using integer inputs
python
length = int(input("Enter length: "))
width = int(input("Enter width: "))
area = length * width
print("Area of rectangle is", area)
Using <code>int()</code> restricts inputs to whole numbers only, which might be simpler but less flexible.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of operations (input, multiplication, output), so it runs in constant time.

Space Complexity

Only a few variables are stored, so the space used is constant regardless of input size.

Which Approach is Fastest?

All approaches run in constant time and space; using a function adds clarity but no performance difference.

ApproachTimeSpaceBest For
Direct calculationO(1)O(1)Simple quick calculation
Function methodO(1)O(1)Reusable code and clarity
Integer inputsO(1)O(1)When only whole numbers are needed
💡
Always convert input strings to numbers with float() or int() before calculations.
⚠️
Forgetting to convert input strings to numbers causes errors or incorrect results.