Python Program to Find Area of a Rectangle

Python Program to Find Area of a Rectangle | Here, we will discuss all possible methods to write a python program to calculate the area of the rectangle. The length and width will be given by the end-user at run-time. Then, the python program will calculate the area of the rectangle using various methods.

Area of Rectangle
area = length * width

Mathematically,

Inputs: length = 5
width = 4
Area of rectangle = 5 x 4 = 20

Now let’s see how we can implement the area of rectangle calculator in Python.

Area of Rectangle in Python

Program description:- Write a Python program to find the area of the rectangle.

This is the simplest and easiest way to calculate the area of the rectangle in python. We will take length and width when declaring the variable, the value of the area will be calculated and stored in the variable, and finally, it will be displayed on the screen.

# python program to find area of rectangle

# take inputs
length = 2
width = 5

# calculate area of rectangle
area = length * width

# display result
print('Area of rectangle = ',area)

Output:-

Area of rectangle = 20

In this program, we have hardcoded the value of length and width in the source code.

length = 2 and width = 5

Now, calculate the area of the rectangle using formula.

area = length * width

Then, the value of the area is displayed using the print() function.

print('Area of rectangle = ',area)

find the area of rectangle where length and width are provided by the user

In the previous program, length and width are hardcoded in the program but in this program, length and width will be provided by the user.

# python program to find area of rectangle

# take inputs
length = float(input('Enter the length of the rectangle: '))
width = float(input('Enter the width of the rectangle: '))

# calculate area of rectangle
area = length * width

# display result
print('Area of rectangle = ',area)

Output:-

Enter the length of the rectangle: 6
Enter the width of the rectangle: 5
Area of rectangle = 30.0

Enter the length of the rectangle: 4.5
Enter the width of the rectangle: 9
Area of rectangle = 40.5

In this program, Inputs (length and width) will be provided by the user. Inputs are scanned using the input() function. input() method reads user input as a string. So, we need to convert it to float first and store it in the variable.

length = float(input('Enter the length of the rectangle: '))
width = float(input('Enter the width of the rectangle: '))

Then, calculate the area of the rectangle using the formula, and the value of the area is displayed using the print() function.

Find Area of Ractangle using Functions

This is a different method to calculate the area of the rectangle program in python. We can also take the help of function. A function is a block of code that performs a specific task.

# python program to find area of rectangle using functions

def area_of_rectangle(length, width):      # user-defined function
 area = length * width     # calculate area of rectangle
 return area            # return value

# store input
length = float(input('Enter the length of the rectangle: '))
width = float(input('Enter the width of the rectangle: '))

# display result
print('Area of rectangle = %.2f ' %area_of_rectangle(length, width))

Output:-

Enter the length of the rectangle: 9.58
Enter the width of the rectangle: 12.5
Area of rectangle = 119.75

In this program, we will be the first defined function. Length and width are scanned using the input() function and stored in variables. Then call the function and print the area of the rectangle.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Also See:- Python Program to Find Area of Circle

Leave a Reply