0% found this document useful (0 votes)
68 views3 pages

P04 - 4. Area of Rectangle, Square, Circle and Triangle

Uploaded by

CRITERION II
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views3 pages

P04 - 4. Area of Rectangle, Square, Circle and Triangle

Uploaded by

CRITERION II
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4.

Program to find the area of rectangle, square, circle and triangle by accepting suitable input parameters
from user.

Aim : Find the area of rectangle, square, circle and triangle by accepting suitable input parameters from user.

Procedure:

Step 1: Open Python IDE.

Step 2: Create a function calculate_area with shape name as parameter.

Step 3: With in the function convert the parameter into lower case letters.

Step 4: Then check the parameter is rectangle, square, circle or triangle and calculate accordingly. Then
Print the Result.

Step 5: In the main program get shape name from the user.

Step 6: Call calculate_area function with shape name as parameter.

Result: Program to find the area of rectangle, square, circle and triangle by accepting suitable input
parameters from user is done successfully.
Program:

# define a function for calculating


# the area of a shapes
def calculate_area(name):

name = name.lower()

if name == "rectangle":
l = int(input("Enter rectangle's length: "))
b = int(input("Enter rectangle's breadth: "))

# calculate area of rectangle


rect_area = l * b
print(f"The area of rectangle is {rect_area}.")

elif name == "square":


s = int(input("Enter square's side length: "))

# calculate area of square


sqt_area = s * s
print(f"The area of square is {sqt_area}.")

elif name == "triangle":


h = int(input("Enter triangle's height length: "))
b = int(input("Enter triangle's breadth length: "))

# calculate area of triangle


tri_area = 0.5 * b * h
print(f"The area of triangle is {tri_area}.")

elif name == "circle":


r = int(input("Enter circle's radius length: "))
pi = 3.14

# calculate area of circle


circ_area = pi * r * r
print(f"The area of circle is {circ_area}.")

else:
print("Sorry! This shape is not available")

# driver code
if __name__ == "__main__" :

print("Calculate Shape Area")


shape_name = input("Enter the name of shape whose area you want to
find: ")
# function calling
calculate_area(shape_name)

Output:
>>>
Calculate Shape Area
Enter the name of shape whose area you want to find: rectangle
Enter rectangle's length: 10
Enter rectangle's breadth: 20
The area of rectangle is 200.
>>>

>>>
Calculate Shape Area
Enter the name of shape whose area you want to find: square
Enter square's side length: 5
The area of square is 25.
>>>

>>>
Calculate Shape Area
Enter the name of shape whose area you want to find: triangle
Enter triangle's height length: 10
Enter triangle's breadth length: 20
The area of triangle is 100.0.
>>>

>>>
Calculate Shape Area
Enter the name of shape whose area you want to find: circle
Enter circle's radius length: 5
The area of circle is 78.5.
>>>

>>>
Calculate Shape Area
Enter the name of shape whose area you want to find: cirlce
Sorry! This shape is not available
>>>

You might also like