0
0
PythonProgramBeginner · 2 min read

Python Program to Find Area of Rectangle

To find the area of a rectangle in Python, multiply length and width using area = length * width. For example, area = 5 * 3 calculates the area as 15.
📋

Examples

Inputlength = 2, width = 4
OutputArea of rectangle is 8
Inputlength = 5, width = 3
OutputArea of rectangle is 15
Inputlength = 0, width = 10
OutputArea of rectangle is 0
🧠

How to Think About It

To find the area of a rectangle, you need two measurements: length and width. The area is the amount of space inside the rectangle, which you get by multiplying length by width using the * operator.
📐

Algorithm

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

Code

python
length = float(input('Enter length of rectangle: '))
width = float(input('Enter width of rectangle: '))
area = length * width
print('Area of rectangle is', area)
Output
Enter length of rectangle: 5 Enter width of 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 = 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 as strings, then convert them to numbers with float().

Step 2: Calculating area

We multiply length and width using * to find the area inside the rectangle.

Step 3: Displaying output

We print the result with a clear message so the user understands the output.

🔄

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)
This uses integers instead of floats, which works if you only need whole numbers.

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 used to store inputs and the result, so space usage is constant.

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 one-time calculation
Function methodO(1)O(1)Reusable code and clarity
Integer inputsO(1)O(1)When only whole numbers are needed
💡
Always convert input to numbers before calculations to avoid errors.
⚠️
Forgetting to convert input strings to numbers causes errors or incorrect results.