Question

In: Computer Science

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

Solutions

Expert Solution

For product using Recursion we use a simple rule that

Product x*y is nothing but adding 'x' to the sum 'y ' times

Source Code:

import java.util.Scanner;

class ex
{
   static int recursiveProduct(int x,int y)
   {
       if(y<=1)
           return x;
       return x+recursiveProduct(x,--y);
   }
   public static void main(String[] args) {
       Scanner input=new Scanner(System.in);
       System.out.print("Enter number 1: ");
       int num1=input.nextInt();
       System.out.print("Enter number 2: ");
       int num2=input.nextInt();
       System.out.println("The product of "+num1+" and "+num2+" is "+recursiveProduct(num1,num2));
   }
}

sample input and output:


Related Solutions

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)] =...
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  ...
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.
Give a recursive algorithm to compute a list of all permutations of a given set S....
Give a recursive algorithm to compute a list of all permutations of a given set S. (That is, compute a list of all possible orderings of the elements of S. For example, permutations({1, 2, 3}) should return {〈1, 2, 3〉, 〈1, 3, 2〉, 〈2, 1, 3〉, 〈2, 3, 1〉, 〈3, 1, 2〉, 〈3, 2, 1〉}, in some order.) Prove your algorithm correct by induction.
Babylonian Algorithm. The Babylonian algorithm to compute the square root of a positive number n is as follows:
Chapter 3 Exercise 1Babylonian Algorithm. The Babylonian algorithm to compute the square root of a positive number n is as follows:      1. Make a guess at the answer (you can pick n/2 as your initial guess).      2. Computer = n / guess.      3. Set guess = (guess +r) / 2.      4. Go back to step 2 until the last two guess values are within 1% of each other.Write a program that inputs an integer for n, iterates through the Babylonian algorithm until the guess...
Describe an optimal algorithm for finding the integers common to two lists of n integers each....
Describe an optimal algorithm for finding the integers common to two lists of n integers each. Evaluate how long each step in your algorithm takes using Θ-notation.
prove 2 is a factor of (n+1)(n+2) for all positive integers
prove 2 is a factor of (n+1)(n+2) for all positive integers
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 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...
use euclidean algorithm to find integers m,n such that 1693m+2019n=1
use euclidean algorithm to find integers m,n such that 1693m+2019n=1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT