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!")

Was this helpful?

India's 1st AI Doubt Solver for CBSE, JEE, and NEET

Ask a Question for Free

and then it's just ₹212 a month

Learn how Chatterbot AI can help you succeed

Cover Image of the article Computer Science Class 11 CBSE - The Ultimate Guide with Notes, Solutions and AI

Computer Science Class 11 CBSE - The Ultimate Guide with Notes, Solutions and AI

This ultimate guide for CBSE Computer Science class 11 has detailed notes, NCERT solutions, cheat sheets, and our free AI-powered doubt-solving assistant, Chatterbot AI.

Cover Image of the article JEE Advanced 2024 Exam Date Announced: Complete Guide to Eligibility, Syllabus, and Preparation Tips

JEE Advanced 2024 Exam Date Announced: Complete Guide to Eligibility, Syllabus, and Preparation Tips

JEE Advanced 2024 on May 26! Get exam schedule, syllabus, prep tips & more in this guide. Ace India's top engineering test with topper strategies.

Cover Image of the article How to Crack NEET: The Ultimate Blueprint to Outsmart the Exam and Unlock Your Medical Dreams

How to Crack NEET: The Ultimate Blueprint to Outsmart the Exam and Unlock Your Medical Dreams

Ace NEET with expert strategies: Discover effective exam strategies, time management, core concepts mastery, problem-solving techniques, revision tips, and AI-assisted doubt clearing with Chatterbot AI.

Cover Image of the article How to Crack IIT: Smart Self-Study Strategies and AI Tools for Success

How to Crack IIT: Smart Self-Study Strategies and AI Tools for Success

Ace IIT JEE in 6 months without coaching. Discover expert self-study strategies for Physics, Chemistry, and Math. Master time management, mock tests, and leverage AI tools like Chatterbot AI for personalized doubt-solving.