Question

In: Computer Science

Implement and complement the recursive code to compute the product of two positive integers, x and...

Implement and complement the recursive code to compute the product of two positive integers, x and y, using only addition and/or subtraction. The function will take as its arguments two integers to multiply together ( x x y ) and will return the product.

Hint: consider the following:

6 x 1 = 6
6 x 2 = 6 + (6 x 1)
6 x 3 = 6 + (6 x 2) = 6 + [6 + (6 x 1)] = 6 + 6 + 6

#include<iostream>
using namespace std;

// Implement this product recursive function.

int product(int x, int y)
{

}

int main()
{
int x = 40, y = 10;
cout << "product(" << x << ", " << y << ")=";
cout << product(x, y) << endl;
return 0;
}

Solutions

Expert Solution


#include <bits/stdc++.h>
using namespace std;

// recursive function to calculate
// multiplication of two numbers
int product(int x, int y)
{
   // if x is less than
   // y swap the numbers
   if (x < y)
       return product(y, x);

   // iteratively calculate
   // y times sum of x
   else if (y != 0)
       return (x + product(x, y - 1));

   // if any of the two numbers is
   // zero return zero
   else
       return 0;
}

// Driver Code
int main()
{
   int x = 5, y = 2;
   cout << product(x, y);
   return 0;
}
----------------------------------------------------------------------------------------------------------------------------------------------------
#include<iostream>

using namespace std;
class GFG
{
  
/* function to multiply two numbers x and y*/
public : int multiply(int x, int y)
{
   /* 0 multiplied with anything gives 0 */
   if(y == 0)
   return 0;

   /* Add x one by one */
   if(y > 0 )
   return (x + multiply(x, y-1));

   /* the case where y is negative */
   if(y < 0 )
   return -multiply(x, -y);
}
};

// Driver code
int main()
{
   GFG g;
   cout << endl << g.multiply(5, -11);
   getchar();
   return 0;
}



Related Solutions

2. Give a recursive algorithm to compute the product of two positive integers, m and n,...
2. Give a recursive algorithm to compute the product of two positive integers, m and n, using only addition and subtraction. Java Language...
in code c++ describe a recursive algorithm for multiplying two nonnegative integers x and y based...
in code c++ describe a recursive algorithm for multiplying two nonnegative integers x and y based on the fact that xy = 2(x · (y/2)) when y is even and xy = 2(x · ⌊y/2⌋) + x when y is odd, together with the initial condition xy = 0 when y = 0.
Write a recursive method pow(x, y) to calculate xy, where x and y are positive integers....
Write a recursive method pow(x, y) to calculate xy, where x and y are positive integers. If x=2, y=4, the method pow should return 16. Java answers only please.
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 *...
Now we need to develop a recursive algorithm for multiplication operation with two positive integers without...
Now we need to develop a recursive algorithm for multiplication operation with two positive integers without using the multiplication operator. Your answer should be in pseudocode or Java code format. Then use the Three-Question Approach to verify your recursive algorithm. No coding is required for this assignment 1. The Base-Case Question Is   there   a   nonrecursive   way   out   of   the   algorithm,   and   does   the   algorithm   work   correctly   for   this   base   case? 2. The Smaller-Caller Question Does   each   recursive   call   to   the  ...
Implement a recursive binary search on an array of 8-byte integers in LEGV8
Implement a recursive binary search on an array of 8-byte integers in LEGV8
Please use java language Thanks! Implement a recursive method called "pow" that takes 2 integers, x...
Please use java language Thanks! Implement a recursive method called "pow" that takes 2 integers, x and y, as parameters and returns the value xy (x raised to the power y). The exponent must be non-negative. If a negative argument is given for the exponent, then an exception should be thrown. Implement a recursive method called "fib" that takes a positive integer, n, as a parameter and returns the nth Fibonacci value. Assume that the first 2 values in the...
1) You must implement a recursive Quicksort algorithm that will read integers from the attached MyList.txt...
1) You must implement a recursive Quicksort algorithm that will read integers from the attached MyList.txt file. Your algorithm must sort the list(integers)in ascending order. 2)You must implement a recursive Mergesort algorithm that will read integers from the attached MyList.txt file. Your algorithm must sort the list(integers)in ascending order. My List.txt Values 7 3 4 1 4 4 9 9 4 8 4 5 3 9 2 3 7 0 6 4 4 5 0 1 9 2 1 7...
1.Using C++ code, write a program named q4.cpp to compute the product (multiplication) of 4 integers...
1.Using C++ code, write a program named q4.cpp to compute the product (multiplication) of 4 integers a, b, c and d, where a is input by a user via the keyboard, b = 2a - 1, c = ab - 2 and d = |a - b - c|. 2.Using C++ code, write a program named q5.cpp to solve the equation 2n = 14.27 for n. 3.Using C++ code, write a program named q3.cpp to divide each of the elements...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Input Format A series of integers Constraints None Output Format...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT