Question

write a Python function to calculate the area and perimeter of the following rectangle, square, and triangle.

def calculate_area (length, width):

return length * width



print (calculate_area (7, 4))

Answers

GPT-4o mini
You can extend the function to calculate the area and perimeter of a rectangle, square, and triangle separately. Here’s an implementation in Python that includes functions for each shape:

```python
def calculate_area_rectangle(length, width):
return length * width

def calculate_perimeter_rectangle(length, width):
return 2 * (length + width)

def calculate_area_square(side):
return side ** 2

def calculate_perimeter_square(side):
return 4 * side

def calculate_area_triangle(base, height):
return 0.5 * base * height

def calculate_perimeter_triangle(side1, side2, base):
return side1 + side2 + base

# Example usage
# Rectangle
length = 7
width = 4
print("Rectangle Area:", calculate_area_rectangle(length, width))
print("Rectangle Perimeter:", calculate_perimeter_rectangle(length, width))

# Square
side = 5
print("Square Area:", calculate_area_square(side))
print("Square Perimeter:", calculate_perimeter_square(side))

# Triangle
base = 6
height = 3
side1 = 5
side2 = 4
print("Triangle Area:", calculate_area_triangle(base, height))
print("Triangle Perimeter:", calculate_perimeter_triangle(side1, side2, base))
```

### Explanation of the Functions:
- `calculate_area_rectangle(length, width)`: Returns the area of a rectangle.
- `calculate_perimeter_rectangle(length, width)`: Returns the perimeter of a rectangle.
- `calculate_area_square(side)`: Returns the area of a square.
- `calculate_perimeter_square(side)`: Returns the perimeter of a square.
- `calculate_area_triangle(base, height)`: Returns the area of a triangle using the formula \( \frac{1}{2} \times base \times height \).
- `calculate_perimeter_triangle(side1, side2, base)`: Returns the perimeter of a triangle using the lengths of all three sides.

The example usage calculates the area and perimeter for each shape and prints the results.

Related Questions