Find Area of Rectangle


Area of Rectangle

To find the area of a rectangle whose length and breadth are given, use the following formula.

Area of Rectangle = length * breadth

Program

In the following program, we shall define a function areaOfRectangle() that takes length and breadth as arguments and return the area of rectangle.

We read length and breadth as integers from user, call areaOfRectangle() with the length and breadth passed as arguments. You can also read length and breadth as floating-point numbers from user using float() function. In the following program we use int() and you may try float() as practice.

Input

length, breadth of rectangle, where length > 0 and breadth > 0.

Python Program

def areaOfRectangle(length, breadth) :
    if length < 0 or breadth < 0:
        print('Length or breadth cannot be negative.')
        return
    return length * breadth

length = int(input('Enter length : '))
breadth = int(input('Enter breadth : '))
area = areaOfRectangle(length, breadth)
print(f'Area of Rectangle : {area}')

Output

Enter length : 5
Enter breadth : 8
Area of Rectangle : 40

Important Points regarding above program

  • input() function reads a string from standard input. Therefore, we have used int() function to convert string to integer.
  • If given length or breadth is less than zero, we are returning nothing.