You are on page 1of 7

GE Assignment 3(CS)

Chapter 2

Q7. Write a function areaTriangle that takes the lengths of three sides: side1, side2,
and side3 of the triangle as the input parameters and returns the area of the triangle
as the output. Also, assert that sum of the lenh of any two sides is greater than the
third side. Write a function main that accepts inputs from the user interactively and
computes the area of the triangle using the function areaTriangle.
CODE:
def areaTriangle(side1, side2, side3):
assert side1 + side2 > side3 or side2 + side3 >side1 or side3 + side1 > side2
s = (side1 + side2 + side3)/2
area = (s*(s-side1)*(s-side2)*(s-side3))**0.5
return area

def main():
side1 = float(input('enter the first side: '))
side2 = float(input('enter the second side: '))
side3 = float(input('enter the third side: '))
print(areaTriangle(side1, side2, side3))

if __name__ == '__main__':
main()

RESULT:
Q8. Create the following scripts importedModule (Fig. 2.17) and mainModule (Fig.
2.18) in the working directory, execute the script mainModule and justify the output.

CODE:

def test1():
print('test1 in imported module ')

def test2():
print('test2 in imported module')

test1()
test2()

import importedmodule
print('hello')

RESULT:
Chapter 3

Q6. Write a function that accepts as an input parameter the number of rows to be printed
and prints a figure like:

a) CODE:
for i in range(1, 6):
for j in range(1, i + 1):
print(j, end=' ')
print( )
RESULT:
b) CODE:
for i in range(1, 5):
for j in range(1, 5 - i):
print(end=" ")
for j in range(i,0,-1):
print( j, end=" ")
for j in range(2,i+1):
print( j, end=" ")
print()

RESULT:

c) CODE:
for i in range(0, 5):
for j in range(5 - i, 0, -1):
print(j, end=' ')
print( )
RESULT:

d) CODE:
for i in range(6):
for j in range(i):
print(i, end=" ")

print( )

RESULT:

e) CODE:
for i in range(1, 6):
for j in range(1, i):
print("", end=" ")
for k in range(i, 6):
print(k, end="")
print( )
RESULT:

f) CODE:
rows = int(input("no. of rows: "))
columns = int(input("no. of columns: "))
for i in range(rows):
for j in range(columns):
if (i == 0 or i == rows - 1 or j == 0 or j == columns - 1):
print('*', end = ' ')
else:
print(' ', end = ' ')
print()
RESULT:

by Suhani Kadian

You might also like