codescracker
python

Python Program to Find Area of Rectangle



« Previous Program Next Program »

In this article, we've created some programs in Python to find and print area of rectangle based on its length and base entered by user. Here are the list of programs:

Formula to Find Area of Rectangle

To find area of a rectangle, use:

area = len*bre

Here area indicates to area value, len indicates to length value and bre indicates to breadth value of rectangle.

Find Area of Rectangle

To calculate area of rectangle in Python, you have to ask from user to enter the length and breadth value of rectangle. Then apply the formula and find its area as shown in the program given below:

print("Enter Length of Rectangle: ")
l = float(input())
print("Enter Breadth of Rectangle: ")
b = float(input())
a = l*b
print("\nArea = ", a)

Here is its sample run:

calculate area of rectangle python

Now supply inputs as length and breadth of a rectangle say 7 as length and 5 as breadth, press ENTER key to find and print the area value as shown in the snapshot given below:

area of rectangle python

Find Area of Rectangle using Function

This program does the same job as of previous program. But this program uses a user-defined function areaOfRect() to find the area value of rectangle.

This function takes two arguments and returns its multiplication. Therefore, we've passed length (l) and breadth (b) as its argument. The value of l and b gets copied to x and y and x*y gets returned by this function, and assigned to a, that is the area value of given rectangle:

def areaOfRect(x, y):
    return x*y

print("Enter Length and Breadth of Rectangle: ", end="")
l = float(input())
b = float(input())
a = areaOfRect(l, b)
print("\nArea = ", a)

Here is its sample run with user input 5.3 as length and 2.4 as breadth of rectangle:

python calculate area of rectangle

To print the area value upto 2 decimal places only, replace the following statement:

print("\nArea = ", a)

with the statement given below:

print("\nArea = {:.2f}".format(a))

Now the output looks like:

python find area of rectangle using function

Find Area of Rectangle using Class

This is the last program of this article. This program uses class and object, an object-oriented feature of Python to find and print area of rectangle.

class CodesCracker:
    def areaOfRect(self, x, y):
        return x*y

print("Enter Length and Breadth of Rectangle: ", end="")
l = float(input())
b = float(input())

cobj = CodesCracker()
a = cobj.areaOfRect(l, b)

print("\nArea = {:.2f}".format(a))

This program produces similar output as of previous program with value upto 2 decimal places only.

We've assigned all the properties of the class CodesCracker to an object named cobj using following statement:

cobj = CodesCracker()

Now through this object, we can access the member function (areaOfRect()) of the class, using dot (.) operator. Rest of the things works like a normal function.

Same Program in Other Languages

Python Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker