Question

Write a recursive method in java to find GCD of two integers using Euclid's method. Integers...

Write a recursive method in java to find GCD of two integers using Euclid's method. Integers can be positive or negative.

public class Recursion {

                public static void main(String[] args) {

                                Recursion r = new Recursion();

                                System.out.println(“The GCD of 24 and 54 is “+r.findGCD(24,54)); //6

                }

                public int findGCD(int num1, int num2){

                                return -1;

                }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class Recursion {

  public static void main(String[] args) {

    Recursion r = new Recursion();

    System.out.println("The GCD of 24 and 54 is "+r.findGCD(24,54)); //6

  }


  public int findGCD(int num1, int num2){
      if (num2 != 0)
        return findGCD(num2, num1%num2);
      else
        return num1;
    }
}

Output:

The GCD of 24 and 54 is 6

Please upvote the solution if it helped. Thanks!

Add a comment
Know the answer?
Add Answer to:
Write a recursive method in java to find GCD of two integers using Euclid's method. Integers...
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
  • DEBUGGING. JAVA. Identify the errors in the following. There are no logical errors in this one...

    DEBUGGING. JAVA. Identify the errors in the following. There are no logical errors in this one just syntax issues. //start import java.util.Scanner; public class NumbersDemo {    public static void main (String args[])    {       Scanner kb = new Scanner(System.in);       int num1, num2;             System.out.print("Enter an integer >> ");       num1 = kb.nextInt();       System.out.print("Enter another integer >> ");       num2 = kb.nextDouble();       displayTwiceTheNumber(num1);       displayNumberPlusFive(num1);       displayNumberSquared(num1)       displayTwiceTheNumber(num2);       displayNumberPlusFive(num2);       displayNumberSquared(num2);    }   ...

  • A palindrome is a word, phrase, number, or other sequence of characters which reads the same...

    A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward as forward, such as madam or racecar. Write a recursive method in java that accepts a integer as its argument and returns true if it is palindrome, false otherwise. public class Recursion {                 public static void main(String[] args) {                                 Recursion r = new Recursion();                                 System.out.println(“Is 12321 a palindrome? “+ra.isPalindrome(12321)); //true                 }                 public Boolean isPalindrome(int num){                                 return false;...

  • What output is produced by the following program? public class MysteryNums public static void main(String[] args)...

    What output is produced by the following program? public class MysteryNums public static void main(String[] args) - int x = 12; int y = x - 3; 3, sentence (y, x + y); . public static void sentence (int numi, int num2) { 4: System.out.println(num1 + " + num2);

  • Could someone re-write this code so that it first prompts the user to choose an option...

    Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...

  • Java, finish the recursive function that converts a binary string to decimal, do not change main...

    Java, finish the recursive function that converts a binary string to decimal, do not change main or function header. public static void main( String[] args ) { if( binToDec( "1101100" ) == 108 ) { System.out.println( "binToDec1 is correct!" ); } if( binToDec( "1011101" ) == 93 ) { System.out.println( "binToDec2 is correct!" ); } } public static int binToDec( String bin ) { }

  • JAVA I need a switch so that users can input one selection then another. These selections...

    JAVA I need a switch so that users can input one selection then another. These selections are for a simple calculator that uses random numbers 1. + 2. - 3. * 4./ 5. RNG. This has to be simple this is a beginners class. Here is what I have import java.util.Scanner; import java.util.Random; import java.util.*; public class You_Michael02Basic_Calculator {    public static void main(String[] args) {        // Generate random numbers        int num1,num2;        num1 =(int)(Math.random()*100+1);...

  • JAVA language Add a recursive method to the program shown in the previous section that states...

    JAVA language Add a recursive method to the program shown in the previous section that states how many times a particular value appears on the stack. Code: class Stack { protected Node top; Stack() { top = null; } boolean isEmpty() { return( top == null); } void push(int v) { Node tempPointer; tempPointer = new Node(v); tempPointer.nextNode = top; top = tempPointer; } int pop() { int tempValue; tempValue = top.value; top = top.nextNode; return tempValue; } void printStack()...

  • Write java class “BinarySearch” that uses recursion to find the index of a target value in...

    Write java class “BinarySearch” that uses recursion to find the index of a target value in an ascending sorted array. If not found, the result is -1. The array has to be unsorted before you sort it. Use “ BinarySearchDriver.java” to drive the “BinarySearch” class /************************************************************* * BinarySearchDriver.java * Student Name * *. *************************************************************/ public class BinarySearchDriver { public static void main(String[] args) {     int[] array = new int[] {55, 88, 33, 5, 8, 12, 16, 23, 45}; /unsorted...

  • Consider the problem of finding the Greatest Common Divisor (GCD) of two positive integers a and...

    Consider the problem of finding the Greatest Common Divisor (GCD) of two positive integers a and b. It can be mathematically proved that if b<=a GCD(a, b) = b, if (a mod b) = 0; GCD(a, b) = GCD(b, a mod b), if (a mod b) != 0. Write a recursive function called GCD with signature “public static int GCD(int a, int b)” that returns the greatest common divisor of two positive integers a and b with b <= a....

  • Looking for some simple descriptive pseudocode for this short Java program (example italicized in bold directly...

    Looking for some simple descriptive pseudocode for this short Java program (example italicized in bold directly below): //Create public class count public class Count {     public static void main(String args[])     {         int n = getInt("Please enter an integer value greater than or equal to 0");                System.out.println("Should count down to 1");         countDown(n);                System.out.println();         System.out.println("Should count up from 1");         countUp(n);     }            private static void countUp(int n)     {...

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
ADVERTISEMENT