Page 241 - Computer Science V2.0 Class 11
P. 241
# Function to calculate the area of a rectangle
def computeRectangleArea(length, breadth):
return length * breadth
# Function to calculate the perimeter of a rectangle
def computeRectanglePerimeter(length, breadth):
return 2 * (length + breadth)
# Function to calculate the area of a triangle
def computeTriangleArea(base, height):
return 0.5 * base * height
# Function to calculate the area of a circle
def computeCircleArea(radius):
return math.pi * radius**2
# Function to calculate the circumference of a circle
def computeCircleCircumference(radius):
return 2 * math.pi * radius
# Function to calculate the surface area of a cylinder
def computeCylinderSurfaceArea(radius, height):
return 2 * math.pi * radius * (radius + height)
while True:
print("\nEnter the choice for shape:")
print("1: Square, 2: Rectangle, 3: Triangle,\
4: Circle: 5:Cylinder, 6: Exit")
choice = int(input(""))
if choice == 1:
side = float(input("Enter the side length of the square: "))
area = computeSquareArea(side)
perimeter = computeSquarePerimeter(side)
print("Area of Square:", area)
print("Perimeter of Square:", perimeter)
elif choice == 2:
length = float(input("Enter the length of the rectangle: "))
breadth = float(input("Enter the breadth of the rectangle: "))
area = computeRectangleArea(length, breadth)
perimeter = computeRectanglePerimeter(length, breadth)
print("Area of Rectangle:", area)
print("Perimeter of Rectangle:", perimeter)
elif choice == 3:
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
area = computeTriangleArea(base, height)
print("Area of Triangle:", area)
Introduction to Functions 227

