6. Implementing programs using Functions. (Factorial, largest number in a list, area of shape)
6. Implementing programs using Functions. (Factorial, largest number in a list, area of shape)
FACTORIAL OF A GIVEN NUMBER
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
n=int(input("Input a number to compute the factiorial : "))
print(factorial(n))
LARGEST NUMBER IN A LIST
def large(arr):
maximum = arr[0]
for ele in arr:
if(ele > maximum):
maximum= ele
return maximum
list1 = []
n=int(input("enter the number of elements"))
for i in range(0,n):
n1=input("enter the elements")
list1.append(n1)
for i in range(0,n):
print(list1[i])
result = large(list1)
print("largest number",result)
AREA OF SHAPE
def square(l):
area=l*l
return area
def rectangle(l,w):
area=l*w
return area
print("Please enter a number to calculate the area of shape")
print("Square = 1")
print("Rectangle = 2")
i = int(input("Enter your choice: "))
if (i==1):
print("calculate the area of Square")
length = int(input("Length: "))
S=square(length)
print("Area of Square is: ", S)
if (i==2):
print("Let's calculate the area of Rectangle")
length = int(input("Length: "))
width = int(input("Width: "))
R=rectangle(length, width)
print("Area of Rectangle is: ", R)
Comments
Post a Comment