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:
- Define functions for calculating the area and perimeter of a circle, rectangle, and triangle.
- Main Function: The
main
function is the entry point of the program. - User Input: Prompt the user to enter the required dimensions for each shape.
- 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 formula2 * π * radius
.area_rectangle(length, width)
: Calculates the area of a rectangle using the formulalength * width
.perimeter_rectangle(length, width)
: Calculates the perimeter of a rectangle using the formula2 * (length + width)
.area_triangle(base, height)
: Calculates the area of a triangle using the formula0.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:
- Save the code in a file named
shapes.py
. - Run the program using the command:
python shapes.py
. - Enter the required dimensions for each shape when prompted.
Explanation of the Python Program:
- 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.
- 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.