Write a program that contains user defined functions to calculate area, perimeter or surface area whichever is applicable for various shapes like square, rectangle, triangle, circle and cylinder. The user defined functions should accept the values for calculation as parameters and the calculated value should be returned. Import the module and use the appropriate functions.
Question
Write a program that contains user defined functions to calculate area, perimeter or surface area whichever is applicable for various shapes like square, rectangle, triangle, circle and cylinder. The user defined functions should accept the values for calculation as parameters and the calculated value should be returned. Import the module and use the appropriate functions.
✨ Free AI Tools for You
Powered by Chatterbot AI
Answer
Below is an example of a Python program that contains user-defined functions to calculate the area, perimeter, or surface area for various shapes such as a square, rectangle, triangle, circle, and cylinder:
# Create a module shapes_calculations.py that contains all the functions
# Save this file as shapes_calculations.py
"""
This module contains functions to calculate area, perimeter,
or surface area for various shapes.
"""
# Function to calculate the area and perimeter of a square
def square(side):
area = side ** 2
perimeter = 4 * side
return area, perimeter
# Function to calculate the area and perimeter of a rectangle
def rectangle(length, width):
area = length * width
perimeter = 2 * (length + width)
return area, perimeter
# Function to calculate the area and perimeter of a triangle
def triangle(base, height):
area = 0.5 * base * height
# perimeter calculation is not accurate without all sides
perimeter = "Perimeter requires all three sides"
return area, perimeter
# Function to calculate the area and perimeter of a circle
def circle(radius):
area = 3.14 * radius ** 2 # Alternatively, use math.pi for precise value
circumference = 2 * 3.14 * radius # Alternatively, use math.pi for precise value
return area, circumference
# Function to calculate the surface area and volume of a cylinder
def cylinder(radius, height):
surface_area = 2 * 3.14 * radius * (radius + height) # Alternatively, use math.pi for precise value
volume = 3.14 * radius ** 2 * height # Alternatively, use math.pi for precise value
return surface_area, volume
# Main program to use the above module
if __name__ == "__main__":
import shapes_calculations as sc
# Display options
print("Select the shape to calculate area and perimeter/surface area:")
print("1. Square")
print("2. Rectangle")
print("3. Triangle")
print("4. Circle")
print("5. Cylinder")
# Get the user's choice
choice = int(input("Enter your choice (1-5): "))
if choice == 1:
side = float(input("Enter the side length of the square: "))
area, perimeter = sc.square(side)
print(f"Area of the square: {area}")
print(f"Perimeter of the square: {perimeter}")
elif choice == 2:
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
area, perimeter = sc.rectangle(length, width)
print(f"Area of the rectangle: {area}")
print(f"Perimeter of the rectangle: {perimeter}")
elif choice == 3:
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
area, perimeter = sc.triangle(base, height)
print(f"Area of the triangle: {area}")
print(f"Perimeter of the triangle: {perimeter} (requires all three sides)")
elif choice == 4:
radius = float(input("Enter the radius of the circle: "))
area, circumference = sc.circle(radius)
print(f"Area of the circle: {area}")
print(f"Circumference of the circle: {circumference}")
elif choice == 5:
radius = float(input("Enter the radius of the cylinder: "))
height = float(input("Enter the height of the cylinder: "))
surface_area, volume = sc.cylinder(radius, height)
print(f"Surface area of the cylinder: {surface_area}")
print(f"Volume of the cylinder: {volume}")
else:
print("Invalid choice!")
India's 1st AI Doubt Solver for CBSE, JEE, and NEET
Ask a Question for Freeand then it's just ₹212 a month