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  ...
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...
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
develop a recursive function to compute LCS (x,y)
develop a recursive function to compute LCS (x,y)
The greatest common divisor of two integers x and y is the largest positive integer that...
The greatest common divisor of two integers x and y is the largest positive integer that evenly divides both. For example, gcd(12,6)=6, gcd(21,14)=7, gcd(31,10)=1, gcd(55,25)=5 Recall that the gcd of two integers is gcd(a,0) = a gcd(a,b) = gcd(b, a%b) Create int gcd(int x, int y). Assume that the two integers are zero or positive. Write the code as a pure function. Put it in a file gcd.c. Of course, you will need to test it. The function Φ(n) counts...
Question 1 Given two integers x and y, the following recursive definition determines the greatest common...
Question 1 Given two integers x and y, the following recursive definition determines the greatest common divisor of x and y, write gcd(xy). Write a recursive function, gcd, that takes two integers as parameters and returns the greatest common divisor of numbers. Also write a program to test your function. Write a recursive function, reverseDigits, that takes an integer as a parameter and returns the number with the digits reversed. Also write a program to test your application.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT