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, 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.
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.
Write a function called alternate that takes two positive integers, n and m, as input arguments...
Write a function called alternate that takes two positive integers, n and m, as input arguments (the function does not have to check the format of the input) and returns one matrix as an output argument. Each element of the n-by-m output matrix for which the sum of its indices is even is 1. All other elements are zero. For example, here is an example run: >> alternate(4,5) ans = 1 0 1 0 1 0 1 0 1 0...
Write a function named “compileStats” that has 4 parameters: a vector of integers, an integer representing...
Write a function named “compileStats” that has 4 parameters: a vector of integers, an integer representing the smallest value contained in the vector, an integer representing the largest value contained in the vector, and a double that represents the average of the values in the vector. The compileStats function will not “return” a value, it will set the Pass By Reference parameters to the correct values (smallest, largest, and average). Use the following main function in driver1b.cpp as a starting...
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and...
Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and returns a second vector that contains only the odd numbers. It will return zero if all elements are even. Use error-traps to check against probable errors in user input. In case of an error, it will return NaN. You are allowed to use Matlab built-in function round(). Check your code with the following arrays: >> y1 = [18, -5, 89, -7, 4, 10, 12,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT