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
(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...
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)...
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...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT