CIS 100 - Introduction To Computer Science: Homework Assignment 4
CIS 100 - Introduction To Computer Science: Homework Assignment 4
Instructions:
Submit your assignments in word to the blackboard.
The format of the output should be same as specified in the sample output
Input to every program is colored in blue and output is in green
Programs
1. Write a program to calculate and display the area of various shapes (Triangle, Square,
Rectangle, Parallelogram, Trapezoid, Circle, Ellipse and Sector). The program should first
display various choices of shapes and then prompt the user to enter the choice of a
shape. Based on the choice, the program should again prompt the user to enter
required inputs to calculate the area of a chosen shape.
This program should define separate functions for each shape in a module file
“areas.py” to calculate area of shapes. These functions have to be called in main
program. (8 points)
def triangle(b,h):
statements
return result
Sample Output:
Triangle – T
Square – S
Rectangle – R
Parallelogram – P
Trapezoid – Z
Circle – C
Ellipse – E
Sector – O
Enter the choice of a shape: R
Enter the width: 5
Enter the height: 3
Area is 15
CODE:
Module file code:
def triangle(b, h):
return (1/2) * b * h
def square(side):
return side * side
def rectangle(b, h):
return b * h
def parallelogram(b, h):
return b * h
def trapezoid(b1, b2, h):
return (1/2) * (b1 * b2) * h
def circle(r):
return 3.14159 * r * r
def ellipse(b, h) :
return 3.14159 * b * h
def sector(r, a):
return (1/2) * r * r * a
Main program file code:
print("Triangle-T")
print("Square-S")
print("Rectangle-R")
print("Parallelogram-P")
print("Trapezoid-Z")
print("Circle-C")
print("Ellipse-E")
print("Sector-O")
shape = input("Enter a choice of shape:")
import areas
if shape == "T":
b = float(input("Enter base value:"))
h = float(input("Enter height value:"))
print("The area is", areas.triangle(b,h))
OUTPUT: