-
Notifications
You must be signed in to change notification settings - Fork 0
/
areas.py
63 lines (54 loc) · 2 KB
/
areas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#----------------------------------------------
# GEOMETRIC SHAPE AREAS
# ======================
#
# This program calculates and displays the areas
# of various geometrical shapes based on user input.
#
# Author: Biswadeep Roy
# File : geometric_areas.py
#----------------------------------------------
import math
def calculate_square_area(side):
return side * side
def calculate_rectangle_area(length, width):
return length * width
def calculate_triangle_area(base, height):
return 0.5 * base * height
def calculate_circle_area(radius):
return math.pi * radius * radius
def calculate_cylinder_area(radius, height):
return 2 * math.pi * radius * height
print("GEOMETRIC SHAPE AREAS")
print("=====================\n")
print("Select the shape to calculate its area:")
shape = input("Square (s)\nRectangle (r)\nCircle (c)\nTriangle (t)\nCylinder (y): ").lower()
if shape == 's':
side = float(input("Enter the side length of the square: "))
area = calculate_square_area(side)
shape_name = "Square"
elif shape == 'r':
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
area = calculate_rectangle_area(length, width)
shape_name = "Rectangle"
elif shape == 'c':
radius = float(input("Enter the radius of the circle: "))
area = calculate_circle_area(radius)
shape_name = "Circle"
elif shape == 't':
base = float(input("Enter the base length of the triangle: "))
height = float(input("Enter the height of the triangle: "))
area = calculate_triangle_area(base, height)
shape_name = "Triangle"
elif shape == 'y':
radius = float(input("Enter the radius of the cylinder: "))
height = float(input("Enter the height of the cylinder: "))
area = calculate_cylinder_area(radius, height)
shape_name = "Cylinder"
else:
area = None
shape_name = None
print("Invalid shape selected.")
if area is not None:
print(f"The area of the {shape_name} is {area:.2f}")