0% found this document useful (0 votes)
334 views

Exercise 6:implementing Programs Using Functions. (Factorial, Largest Number in A List, Area of Shape)

This document discusses implementing functions in Python to calculate factorials, find the largest number in a list, and calculate the areas of different shapes. It includes code to: 1) Define a factorial function that calculates the factorial of a given number. 2) Use different methods like sorting and the max() function to find the largest number in a list. 3) Define a calculate_area function that takes a shape as a parameter, gets required inputs from the user, and calculates and prints the area of different shapes like rectangle, square, triangle, circle, and parallelogram.

Uploaded by

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

Exercise 6:implementing Programs Using Functions. (Factorial, Largest Number in A List, Area of Shape)

This document discusses implementing functions in Python to calculate factorials, find the largest number in a list, and calculate the areas of different shapes. It includes code to: 1) Define a factorial function that calculates the factorial of a given number. 2) Use different methods like sorting and the max() function to find the largest number in a list. 3) Define a calculate_area function that takes a shape as a parameter, gets required inputs from the user, and calculates and prints the area of different shapes like rectangle, square, triangle, circle, and parallelogram.

Uploaded by

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

Exercise 6:Implementing programs using Functions.

(Factorial, largest number in


a list, area of shape)
a,FACTORIAL

num=int(input("enter the number:"))

factorial = 1

if num < 0:

print("Sorry, factorial does not exist for negative numbers")

elif num == 0:

print("The factorial of 0 is 1")

else:

for i in range(1,num + 1):

factorial = factorial*i

print("The factorial of",num,"is",factorial)

b,LARGEST NUMBER IN A LIST

METHOD 1:

list1 = [10, 20, 4, 45, 99]

list1.sort()

print("Largest element is:", list1[-1])

OUTPUT

Largest element is: 99

METHOD 2:

list1 = [10, 20, 4, 45, 99]

print("Largest element is:", max(list1))

OUTPUT
Largest element is: 99

METHOD 3:
list1 = []

num = int(input("Enter number of elements in list: "))

for i in range(1, num + 1):


ele = int(input("Enter elements: "))
list1.append(ele)

# print maximum element


print("Largest element is:", max(list1))

OUTPUT
Enter number of elements in list: 4
Enter elements: 12
Enter elements: 19
Enter elements: 1
Enter elements: 99
Largest element is: 99

C, Area Of Shape

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 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 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: 15
The area of rectangle is 150.

You might also like