Question

Recursion in Java Write a recursive method that will find the greatest common divisor of two...

Recursion in Java

Write a recursive method that will find the greatest common divisor of two numbers. Use %.

Example of the math:

Finding GCD for 14 and 48:

14 / 48  = 3 
     42
     -----
      6  Remainder
Remainder is not yet zero, so we will now divide 14 by 6

      6 / 14 = 2
          12
          -----
           2 Remainder

Remainder is not yet zero, so we will now divide 6 by 2


           2 / 6 = 3 answer
               6
               ----
               0  Remainder

Remainder is 0 so, we stop here

To Do:

Homework Answers

Answer #1

//Java Code

public class GCD {
    public static void main(String[] args)
    {
        System.out.println(findGCD(14,48));
    }

    /**
     * find the gcd as
     * if a= 14 and b=48
     * a%b = 14%48 = 6
     * and we call the recursive function as
     *  findGCD(b,a%b)
     *  so now a = 48 and b = 14
     *  In next recursive call
     *  a= 14 and b= 6
     *  In next recursive call
     *  a = 6 and b =2
     *  in next step since b=0
     *  so it returns a = 2
     * @param a
     * @param b
     * @return gcd of a,b
     */
    public static int findGCD(int a, int b)
    {
        if(b==0)
            return a;
        else {
          

            return findGCD(b, a % b);
        }
    }
}

//Output

//If you need any help regarding this solution.... please leave a comment.. thanks

Know the answer?
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
Similar Questions
Find the greatest common divisor of the given polynomials over the given field. Then write the...
Find the greatest common divisor of the given polynomials over the given field. Then write the greatest common divisor as a linear combination of the given polynomials. That is, given f(x) and g(x), find a(x) and b(x) so that d(x) = a(x)f(x) + b(x)g(x), where d(x) is the greatest common divisor of f(x) and g(x). (a) x^10 − x^7 − x^5 + x^3 + x^2 − 1 and x^8 − x^5 − x^3 + 1 over Q. (b) x^5 +...
Write a complete recursive java program to compute the heights of the tick marks on a...
Write a complete recursive java program to compute the heights of the tick marks on a ruler. Assume that the length of the ruler is a power of 2 (say L=2^n, where n >=1) , and the marks are to be placed at every point between 0 and 2^n, not including the endpoints. The endpoints 0 and 2^n will have height 0. Here are the rules for computing the heights of the ticks for a ruler of length L=2^n: 1....
---------------- Overview: ---------------- This lab will consist of several independent exercises that must all use recursion...
---------------- Overview: ---------------- This lab will consist of several independent exercises that must all use recursion to solve various problems. Some problems will have fairly short solutions, but declaring and using classes/objects is still required. Some could benefit from having some kind of data structure being passed between calls. Feel free to use your LinkedList if you think it could help. Some solutions will be a mix of iteration and recursion, but all solution need to be recursive in some...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop...
Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop to print the numbers from 42 - 56. Uses a for loop to print the numbers from 87 - 95. Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive. Note: Consider that your user's second number can be lower! (see example below) Note: Also consider that your user might give you two of the same...
Here is a modification of the BST program that includes a recursive find method: BinarySearchTree2C.java (posted...
Here is a modification of the BST program that includes a recursive find method: BinarySearchTree2C.java (posted below) Implement the following methods using recursion: int depth() // returns the length of the deepest path from root to any leaf int node_count() // returns the number of nodes in the tree void insert(int n) // inserts value n into the tree BinarySearchTree2C clone() // returns a clone (deep copy) of the tree Add code to the main method to test these methods....
Comparing two drugs . Makers of generic drugs must show that they do not differ significantly...
Comparing two drugs . Makers of generic drugs must show that they do not differ significantly from the “reference” drugs that they imitate. One aspect in which drugs might differ is their extent of absorption in the blood. We have some data taken from 20 healthy nonsmoking male subjects for one pair of drugs. This is a matched pairs design. Numbers 1 to 20 were assigned at random to the subjects. Subjects 1 to 10 received the generic drug first,...
I need the actual code for this... I found a similar response but it was not...
I need the actual code for this... I found a similar response but it was not thorough enough. Problem Prerequisites: None Suppose that a scientist is doing some important research work that requires her to use rabbits in her experiments. She starts out with one adult male rabbit and one adult female rabbit. At the end of each month, a pair of adult rabbits produces one pair of offspring, a male and a female. These new offspring will take one...
Assessment Identify the Variables! In rotational kinematics - the variables are: t = time, which is...
Assessment Identify the Variables! In rotational kinematics - the variables are: t = time, which is measured in s (for seconds) θ = angle = what angle did the object turn thru, usually measured radians ωO = initial angular velocity = the rotational speed of the object at the beginning of the problem, which is measured in rad/s ω = final angular velocity = the rotational speed of the object at the end of the problem, which is measured in...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT