Write A Python Program To Obtain Length And Breadth Of A Rectangle And Calculate Its Area

Introduction

A rectangle is a four-sided shape with four right angles. The length of a rectangle is the longer side, and the breadth is the shorter side. The area of a rectangle is the amount of space that it encloses.

Python program to obtain length and breadth of a rectangle and calculate its area

Python
def calculate_area_of_rectangle(length, breadth):
  """Calculates the area of a rectangle given its length and breadth.

  Args:
    length: The length of the rectangle.
    breadth: The breadth of the rectangle.

  Returns:
    The area of the rectangle.
  """

  area = length * breadth
  return area

# Example usage:

length = 10
breadth = 5

area = calculate_area_of_rectangle(length, breadth)

print(f"The area of the rectangle is {area}.")

Output:

The area of the rectangle is 50.

Extended example

The following Python program allows the user to input the length and breadth of a rectangle and calculate its area. The program also validates the user input to ensure that both the length and breadth are positive integers.

Python
def calculate_area_of_rectangle(length, breadth):
  """Calculates the area of a rectangle given its length and breadth.

  Args:
    length: The length of the rectangle.
    breadth: The breadth of the rectangle.

  Returns:
    The area of the rectangle.
  """

  if length < 0 or breadth < 0:
    raise ValueError("The length and breadth of the rectangle must be positive integers.")

  area = length * breadth
  return area

# Get the length and breadth of the rectangle from the user.
length = int(input("Enter the length of the rectangle: "))
breadth = int(input("Enter the breadth of the rectangle: "))

# Calculate the area of the rectangle.
area = calculate_area_of_rectangle(length, breadth)

# Print the area of the rectangle.
print(f"The area of the rectangle is {area}.")

Output:

Enter the length of the rectangle: 10
Enter the breadth of the rectangle: 5
The area of the rectangle is 50.

Applications of the Python program

The Python program to obtain length and breadth of a rectangle and calculate its area can be used in a variety of applications, such as:

  • Calculating the area of a room or a garden.
  • Calculating the amount of material needed to build a wall or a fence.
  • Calculating the area of a plot of land.
  • Calculating the area of a billboard or a poster.
  • Calculating the area of a pizza or a cake.

Conclusion

The Python program to obtain length and breadth of a rectangle and calculate its area is a simple and versatile tool that can be used for a variety of tasks.

Post a Comment

0 Comments