A) Write a function cumulative_product to compute cumulative product of a list
of numbers.
B) Write a function reverse to reverse a list. Without using the reverse
function.
C) Write function to compute gcd, lcm of two numbers.
A) AIM: Write a function cumulative_product to compute cumulative product of a
list of numbers.
SOURCE CODE:
def cumulative_product(numlist):
product=1
cp=[]
for ele in numlist:
product=product*ele
cp.append(product)
return cp
numlist=[]
n=int(input("Enter number of elements to be insert:"))
for i in range(n):
ele=int(input("Enter element:"))
numlist.append(ele)
cp=cumulative_product(numlist)
print("Cumulative product of list elements is ",cp)
OUTPUT:
Enter number of elements to be insert:4
Enter element:3
Enter element:2
Enter element:4
Enter element:1
Cumulative product of list elements is 24
B) AIM: Write a function reverse to reverse a list. Without using the reverse
function.
SOURCE CODE:
def reverse(numlist):
i=0
j=len(numlist)-1
while(i<=j):
temp=numlist[i]
numlist[i]=numlist[j]
numlist[j]=temp
i=i+1
j=j-1
return
numlist=[]
n=int(input("Enter number of elements to be insert:"))
for i in range(n):
ele=int(input("Enter element:"))
numlist.append(ele)
reverse(numlist)
print("After reverse list elements are:\n",numlist)
OUTPUT:
Enter number of elements to be insert:4
Enter element:2
Enter element:3
Enter element:1
Enter element:5
After reverse list elements are:
[5, 1, 3, 2]
C) AIM: Write function to compute gcd, lcm of two numbers.
SOURCE CODE:
gcd=lambda a,b: a if b==0 else gcd(b,a%b)
lcm=lambda a,b: (a*b)/gcd(a,b)
num1=int(input("Enter first number:"))
num2=int(input("Enter second number:"))
print("LCM of %d and %d is %d"%(num1,num2,lcm(num1,num2)))
print("GCD of %d and %d is %d"%(num1,num2,gcd(num1,num2)))
OUTPUT:
Enter first number:8
Enter second number:24
LCM of 8 and 24 is 24
GCD of 8 and 24 is 8