Write an iterative function that multiplies two given numbers without using the * operator. You can use the addition operator. (Use for or while)
#Iterative Using while loop
def mult(x,y):
result = 0
ctr = 0
while (ctr < y ):
result = result + x
ctr = ctr + 1
return result
mult(4,3)
Write a recursive function that multiplies two given numbers.
# Recursive function for multiplying numbers
def mult(x,y):
if y == 0:
return 0 #Base Case
else:
return x + mult(x, y-1) #Recursive Case
mult(4,3)
Write a recursive function power that takes two parameters and raises the first to the second. For example, power (4,3) is 444
move to powerpoint
# Recursive function for finding power
def power(x,y):
if y == 0:
return 1 #BASE CASE
else:
return x * power(x, y-1) #RECURSIVE CASE
power(4,3)
#Write a recursive power using mult
def power_m(x,y):
if y == 0:
return 1 #Base Case
else:
return mult(x, power_m(x, y-1)) #Recursive Case
power_m(5,3)