Python Program to Calculate Area and Perimeter of Different Shapes

This page contains a Python program that calculates the area and perimeter of different shapes (circle, rectangle, triangle). The program uses functions to perform these calculations.

Program Explanation

The Python program to calculate the area and perimeter of different shapes follows these steps:

  1. Define functions for calculating the area and perimeter of a circle, rectangle, and triangle.
  2. Main Function: The main function is the entry point of the program.
  3. User Input: Prompt the user to enter the required dimensions for each shape.
  4. Call Functions: Call the appropriate functions to calculate and print the area and perimeter for each shape.

Python Program Code

import math

def area_circle(radius):
    """Calculate the area of a circle."""
    return math.pi * radius ** 2

def perimeter_circle(radius):
    """Calculate the perimeter (circumference) of a circle."""
    return 2 * math.pi * radius

def area_rectangle(length, width):
    """Calculate the area of a rectangle."""
    return length * width

def perimeter_rectangle(length, width):
    """Calculate the perimeter of a rectangle."""
    return 2 * (length + width)

def area_triangle(base, height):
    """Calculate the area of a triangle."""
    return 0.5 * base * height

def perimeter_triangle(side1, side2, side3):
    """Calculate the perimeter of a triangle."""
    return side1 + side2 + side3

def main():
    # Circle
    radius = float(input("Enter the radius of the circle: "))
    print(f"Area of the circle: {area_circle(radius):.2f}")
    print(f"Perimeter of the circle: {perimeter_circle(radius):.2f}")
    
    # Rectangle
    length = float(input("Enter the length of the rectangle: "))
    width = float(input("Enter the width of the rectangle: "))
    print(f"Area of the rectangle: {area_rectangle(length, width):.2f}")
    print(f"Perimeter of the rectangle: {perimeter_rectangle(length, width):.2f}")
    
    # Triangle
    base = float(input("Enter the base of the triangle: "))
    height = float(input("Enter the height of the triangle: "))
    side1 = float(input("Enter the first side of the triangle: "))
    side2 = float(input("Enter the second side of the triangle: "))
    side3 = float(input("Enter the third side of the triangle: "))
    print(f"Area of the triangle: {area_triangle(base, height):.2f}")
    print(f"Perimeter of the triangle: {perimeter_triangle(side1, side2, side3):.2f}")

if __name__ == "__main__":
    main()

Program Details

The program contains the following functions:

  • area_circle(radius): Calculates the area of a circle using the formula π * radius^2.
  • perimeter_circle(radius): Calculates the perimeter (circumference) of a circle using the formula 2 * π * radius.
  • area_rectangle(length, width): Calculates the area of a rectangle using the formula length * width.
  • perimeter_rectangle(length, width): Calculates the perimeter of a rectangle using the formula 2 * (length + width).
  • area_triangle(base, height): Calculates the area of a triangle using the formula 0.5 * base * height.
  • perimeter_triangle(side1, side2, side3): Calculates the perimeter of a triangle by summing the lengths of its sides.

Running the Program

To run the program:

  1. Save the code in a file named shapes.py.
  2. Run the program using the command: python shapes.py.
  3. Enter the required dimensions for each shape when prompted.

 

Explanation of the Python Program:

  1. Function Definitions:
    • area_circle(radius): Calculates the area of a circle.
    • perimeter_circle(radius): Calculates the perimeter (circumference) of a circle.
    • area_rectangle(length, width): Calculates the area of a rectangle.
    • perimeter_rectangle(length, width): Calculates the perimeter of a rectangle.
    • area_triangle(base, height): Calculates the area of a triangle.
    • perimeter_triangle(side1, side2, side3): Calculates the perimeter of a triangle.
  2. Main Function:
    • Prompts the user to enter the radius for the circle and prints the calculated area and perimeter.
    • Prompts the user to enter the length and width for the rectangle and prints the calculated area and perimeter.
    • Prompts the user to enter the base, height, and three sides for the triangle and prints the calculated area and perimeter.

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)