Calculate The Area Of A Rectangle

Python code is a simple program designed to calculate and display the area of a rectangle based on user input. It begins by defining a function called calculate_rectangle_area, which takes two parameters representing the length and width of a rectangle. Within this function, the area is computed using the formula area = length * width, and the result is returned. The main part of the program involves interacting with the user. It prompts the user to input the length and width of the rectangle, converts the input to floating-point numbers, and then calls the calculate_rectangle_area function with the provided values. The calculated area is stored in a variable, and the program concludes by printing a formatted string that includes the user-supplied length and width along with the computed area. In essence, the code demonstrates the use of functions for encapsulating a specific task and showcases basic user interaction in a Python program for calculating the area of a rectangle.


Source code

def calculate_rectangle_area(length, width):
    area = length * width
    return area

# Get input from the user for length and width
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))

# Call the function to calculate the area
area_of_rectangle = calculate_rectangle_area(length, width)

# Display the result
print(f"The area of the rectangle with length {length} and width {width} is: {area_of_rectangle}")

Description

  1. Function Definition:
    • A function named calculate_rectangle_area is defined, which takes two parameters (length and width).
    • Inside the function, it calculates the area of a rectangle using the formula area = length * width and returns the result.
  2. User Input:
    • The program prompts the user to input the length and width of a rectangle using the input function.
    • The inputs are converted to floating-point numbers using float().
  3. Function Call:
    • The calculate_rectangle_area function is called with the user-provided length and width as arguments.
    • The result is stored in the variable area_of_rectangle.
  4. Result Display:
    • The program prints the calculated area of the rectangle using a formatted string, including the provided length and width.
  5. Example Execution:
    • When the program is run, it interacts with the user, calculates the area, and displays the result.

Overall, this code is a simple example of how functions can be used to encapsulate a task (calculating the area of a rectangle) and how user input can be processed in a basic Python program.


Screenshot


Download

By Admin

Leave a Reply

Your email address will not be published. Required fields are marked *