We are going to make a Python program for Calculating Areas Of some mathematical Shapes.
Example:
Input: shape name = "Rectangle" length = 10 breadth = 15 Output: Area: 150 Input: shape name = "Square" side = 10 Output: Area: 100
Approach:
In this program, We will ask the user to input the shape’s name. If it exists in our program then we will proceed to find the entered shape’s area according to their respective formulas. If that shape doesn’t exist then we will print “Sorry! We cannot find this shape.” message on the screen.
Below is the implementation:
Python3
# define a function for calculating # the area of a shapes def calculate_area(name):\ # converting all characters # into lower cases name = name.lower() # check for the conditions if name = = "rectangle" : l = int ( input ( "Enter rectangle's length: " )) b = int ( input ( "Enter rectangle's breadth: " )) # calculate area of rectangel 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 triangle is {circ_area}.") elif name = = 'parallelogram' : b = int ( input ( "Enter parallelogram's base length: " )) h = int ( input ( "Enter parallelogram's height length: " )) # calculate area of parallelogram para_area = b * h print (f"The area of parallelogram is {para_area}.") else : print ( "Sorry! This shape is not availabel" ) # 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: 15 The area of rectangle is 150.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.