Question

In: Computer Science

Write a recursive function named multiply that takes two positive integers as parameters and returns the...

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++

Solutions

Expert Solution

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:


Related Solutions

Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the...
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the matrices are same or 0 otherwise. Set appropriate parameters and return type if necessary.
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the...
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at...
Write a Scheme function that takes a list of integers and returns all odd integers on...
Write a Scheme function that takes a list of integers and returns all odd integers on the list in the original order: (odd-numbers `(2 4 9 16 25 7)) (9 25 7) Hint: 2 (remainder 13 5) 3 Please explain every step
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a function that takes a list of integers as input and returns a list with...
Write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2] Do not use any special or built in functions like append, reverse etc.
C++ write a function called divideBy that takes two integers as its input and returns the...
C++ write a function called divideBy that takes two integers as its input and returns the remainder. If the divisor is 0, the function should return -1, else it should return the remainder to the calling function.
Write a Scheme function that takes two integers and returns the list of all integer numbers...
Write a Scheme function that takes two integers and returns the list of all integer numbers between these two integers (inclusively) in increasing order. (numbers 10 20) (10 11 12 13 14 15 16 17 18 19 20) Please explain every step.
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates to string s with all occurrences of c removed. The string c is guaranteed to be a length-1 string; in other words a single character string. For example (remove-char "abc" "b") should evaluate to "ac". Here is pseudocode that you could implement.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT