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
ARM assembly Code The Euclidean algorithm is a way to find the greatest common divisor of...
ARM assembly Code The Euclidean algorithm is a way to find the greatest common divisor of two positive integers, a and b. First let me show the computations for a=210 and b=45. Divide 210 by 45, and get the result 4 with remainder 30, so 210=4·45+30. Divide 45 by 30, and get the result 1 with remainder 15, so 45=1·30+15. Divide 30 by 15, and get the result 2 with remainder 0, so 30=2·15+0. The greatest common divisor of 210...
(a) Show that the following algorithm computes the greatest common divisor g of the positive integers...
(a) Show that the following algorithm computes the greatest common divisor g of the positive integers a and b, together with a solution (u, v) in integers to the equation au + bv = gcd(a, b). 1. Set u = 1, g = a, x = 0, and y = b 2. If y = 0, set v = (g − au)/b and return the values (g, u, v) 3. Divide g by y with remainder, g = qy +...
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....
Try to write a BNF like Grammar for each one of the following constructs: - positive...
Try to write a BNF like Grammar for each one of the following constructs: - positive and negative integer numbers - floating point numbers - variable names. Notice: Recursion is needed here (use right recursion): here is an example (a grammar of two rules defines any positive integer) : Note the length in digits is not defined in this grammar, it can go recursively for ever. So we need to add another annotation (a semantic part). <positive_Number> -> <digit> |...
language: JAVA the Problem Below are a series of problems you need to solve using recursive...
language: JAVA the Problem Below are a series of problems you need to solve using recursive methods. You will write a program that will read commands from an input file, with each command referring to one of the recursive problems to be executed. Each command will be followed (on the same line of input) by the respective parameters required for that problem. (15 points for main method) DescArrayCheck   Write a recursive method that checks whether an array of integers -...
---------------- 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...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using,...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and...
See four problems attached. These will ask you to think about GCDs and prime factorizations, and...
See four problems attached. These will ask you to think about GCDs and prime factorizations, and also look at the related topic of Least Common Multiples (LCMs). The prime factorization of numbers can be used to find the GCD. If we write the prime factorization of a and b as a = p a1 1 p a2 2 · p an n b = p b1 1 p b2 2 · p bn n (using all the primes pi needed...
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)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT