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.
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.
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 function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++
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.
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, 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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT