Question

PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the...

PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the result of integer division (i.e., 8 / 3 = 2). Please comment on every line of code.

EXISITNG CODE:

int Exponentiation(int X, int Y) {
    if (Y == 0) // base case
        return 1;
    else    // recursive case
        return X * Exponentiation(X, Y - 1);    //make recursive call
}
int Multiply(int X, int Y){
    if(Y == 0){
        return 0;
    }
    else{
        return X + Multiply(X, Y-1);
    }
}
int MultiplyRange(int X, int Y){
    if(Y == 0){
        return 0; 
    }
    else{
        return X + MultiplyRange(X, Y-1);
    }
}
int Subtraction(int X, int Y) {
    if (Y == 0) // base case
        return X;
    else    // recursive case
        return Subtraction(X, Y - 1) - 1;    //make recursive call
}
int Addition(int X, int Y) {
    if (Y == 0) // base case
        return X;
    else    // recursive case
        return 1 + Addition(X, Y - 1);    //make recursive call
}
int AddToN(int n) {
    if (n == 0) {   // base case
        return 0;
    } else { // recursive case
        return n + AddToN(n-1); //make recursive call
    }
}

void printStars (int n)

{

if (n==0 ) {

//base case
cout <<"";

}

else{

printStars(n-1);
cout << "";
}

}

}

int sumRange ( int min, int max)

{

if (min == max){
//base case

return min;
}

else{
//recursive case

return max + sumRange(min,max-1);

}

}

}

int sumDigits(int n)

{

int rightDigit = n%10;

int remainDigits = n / 10;

if(n < 10)

{

// Base case

return n;

}

else

{

return sumDigits(remainDigits) + rightDigit;

}

}

 
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int Division(int X, int Y) {
    if (X < Y) // base case
        return 0;
    else    // recursive case
        return 1 + Division(X-Y, Y);    //make recursive call
}

int main() {
    cout << Division(8, 3) << endl;  // prints 2
    return 0;
}

Please upvote this answer. Let me know if you have doubts..

Add a comment
Know the answer?
Add Answer to:
PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PROBLEM: Write a recursive method named Addition that takes two integers X and Y returns their...

    PROBLEM: Write a recursive method named Addition that takes two integers X and Y returns their sum. Please comment on every line of code. EXISTING CODE: int AddToN(int n) { if (n == 0) { // base case return 0; } else { // recursive case return n + AddToN(n-1); //make recursive call } } void printStars (int n) { if (n==0 ) { //base case cout <<""; } else{ printStars(n-1); cout << ""; } } } int sumRange (...

  • PROBLEM: Write a recursive method named SumEvens that takes an integer X and returns the sum...

    PROBLEM: Write a recursive method named SumEvens that takes an integer X and returns the sum of even digits in X. Please comment on every line of code. EXISTING CODE: int numTwos(int n) { int right = n % 10; int remain = n / 10; if(n==0) { return 0 ; } else if(right ==2) { //rightmost digit is 2 return 1 + numTwos(remain); else { return 0 + numTwos(remain); } } int SumDigits(int X) { if (X == 0)...

  • PROBLEM: Write a recursive method named AddToN that adds up all the numbers between 1 and...

    PROBLEM: Write a recursive method named AddToN that adds up all the numbers between 1 and an integer N. Please add comments to every line of code. EXISTING CODE: void printStars (int n) { if (n==0 ) { //base case cout <<""; } else{ printStars(n-1); cout << ""; } } } int sumRange ( int min, int max) { if (min == max){ //base case return min; } else{ //recursive case return max + sumRange(min,max-1); } } } int sumDigits(int...

  • In C++, write a recursive function power that takes two positive integers base and n as...

    In C++, write a recursive function power that takes two positive integers base and n as inputs and computes base to the n power. Example power(3, 2) is 9. Do not use any loops in your program. This is what I got so far but I'm not sure what I'm doing wrong: #include <iostream> using namespace std; int calc(int x, int y); int main() {       calc(2, 3);    return 0; } int calc() {          cout...

  • C++ Recursion Practice! 1)Multiplication #include <iostream.h> int Multiply(int M, int N) //Performs multiplication using the +...

    C++ Recursion Practice! 1)Multiplication #include <iostream.h> int Multiply(int M, int N) //Performs multiplication using the + operator. //Pre : M and N are defined and N > 0. //Post: Returns M x N { int Prod; if (N == 1)     Prod = M;                       //base case else     Prod = M + Multiply(M, N - 1); //recursive step return Prod; } 2) Reverse #include <iostream.h> void Reverse(int N) //Displays string of length N in the reverse order //Pre : N...

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

  • RecursiveExponent.java Write a recursive function that accepts two arguments into parameters x and y, and which...

    RecursiveExponent.java Write a recursive function that accepts two arguments into parameters x and y, and which returns the value of x raised to the power y. Hint: exponentiation is equivalent to performing repetitions of a multiplication. For example, the base 4 raised to the 6th power = 4*4*4 * 4 * 4 * 4 = 4,096. The only file that you need to create is RecursiveExponent.java. Your main method should prompt the user to supply the base and exponent values...

  • How convert this c++ into C #include<iostream> #include <stdlib.h> using namespace std; void multiplication() { int...

    How convert this c++ into C #include<iostream> #include <stdlib.h> using namespace std; void multiplication() { int num1; int c, num2, ans; cout<<"Enter difficulty level(1/2)\n"; cin>>c; if(c==1) { num1= rand() % 10; num2= rand() % 10; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans while(ans!=(num1*num2)) { if(ans!=(num1*num2)) { cout<< "No. Please try again.\n"; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans; } } } else if(c==2) { num1= rand() % 10+90; num2= rand() % 10+90; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans; while(ans!=(num1*num2)) { if(ans!=(num1*num2)) { cout<< "No. Please...

  • How to convert C++ code to C #include<iostream> #include <stdlib.h> using namespace std; void multiplication() {...

    How to convert C++ code to C #include<iostream> #include <stdlib.h> using namespace std; void multiplication() { int num1; int c, num2, ans; cout<<"Enter difficulty level(1/2)\n"; cin>>c; if(c==1) { num1= rand() % 10; num2= rand() % 10; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans while(ans!=(num1*num2)) { if(ans!=(num1*num2)) { cout<< "No. Please try again.\n"; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans; } } } else if(c==2) { num1= rand() % 10+90; num2= rand() % 10+90; cout<<"what is"<<num1 <<"*"<<num2<<"?\n" ; cin>>ans; while(ans!=(num1*num2)) { if(ans!=(num1*num2)) { cout<< "No....

  • Write a java recursive method digitMatch that takes two nonnegative integers as parameters and that returns...

    Write a java recursive method digitMatch that takes two nonnegative integers as parameters and that returns the number of digits that match between them. Two digits match if they are equal and have the same relative position starting from the end of the number (i.e., starting with the ones digit). In other words, the method should compare the last digits of each number, the second-to-last digits of each number, the third-to-last digits of each number, and so forth, counting how...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT