In: Computer Science
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track:
7 * 4 = 7 + (7 * 3)
7 * 3 = 7 + (7 * 2)
7 * 2 = 7 + (7 * 1)
7 * 1 = 7
So, 7 * 4 = 7 + (7 + (7 + (7)))
The file must be named: multiply.py Must use Python, NOT C++
The Python Program to find the product of two positive integers is as follows:
*********************************************************************************************************************************************
#function multiply to find product of x and y
#recursively
def multiply(x,y): # multiply function starts here
if(y!=0): # if y is not equal to zero then call the function
multiply recursively
return(x+multiply(x,y-1))
else:
#Terminating condition if y is zero return 0
return 0
x=input("Enter 1st Number : ") #Prompting the user to give first
number
y=input("Enter 2nd Number : ") #Prompting the user to give second
number
x=int(x)
y=int(y)
print("The product is :",multiply(x,y)) #printing the product by
calling function multiply
*********************************************************************************************************************************************
The screen shot of the program is as follows
********************************************************************************************************************************************
The output of the program is as follows:
Enter first number: 12
Enter second number: 5
Product is: 60
********************************************************************************************************************************************
The screen shot of the output is as follows: