1+ # Program to calculate the areas of 2d shapes
2+
3+ import math
4+
5+ def square (side ):
6+ area = side * side
7+ return area
8+
9+ def rectangle (length , breadth ):
10+ area = length * breadth
11+ return area
12+
13+ def triangle (side1 , side2 , side3 ):
14+ s = (side1 + side2 + side3 )/ 2
15+ area = math .sqrt (s * (s - side1 )* (s - side2 )* (s - side3 ))
16+ return area
17+
18+ def circle (radius ):
19+ area = 3.14 * radius * radius
20+ return area
21+
22+ final_area = 0.0
23+ print ("Choose the shape you want to calculate area of: " )
24+
25+ while True :
26+ print ("Square, Rectangle, Triangle, Circle" )
27+ shape = input ('>> ' )
28+ print (shape .lower ())
29+
30+ if shape .lower () == "square" :
31+ side = float (input ("Enter the value of side: " ))
32+ final_area = square (side )
33+ break
34+
35+ elif shape .lower () == "rectangle" :
36+ length = float (input ("Enter value of length: " ))
37+ breadth = float (input ("Enter value of breadth: " ))
38+ final_area = rectangle (length , breadth )
39+ break
40+
41+ elif shape .lower () == "triangle" :
42+ side1 = float (input ("Enter the value of 1st side: " ))
43+ side2 = float (input ("Enter the value of 2nd side: " ))
44+ side3 = float (input ("Enter the value of 3rd side: " ))
45+ final_area = triangle (side1 , side2 , side3 )
46+ break
47+
48+ elif shape .lower () == "circle" :
49+ radius = float (input ("Enter the value of radius: " ))
50+ final_area = circle (radius )
51+ break
52+
53+ else :
54+ print ("Please choose a shape from the given 4 or check your spelling again" )
55+
56+ print (f"The area of the shape is: { final_area } " )
0 commit comments