Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Need help with Java Errors, So many errors so little time. Short code?

Need help with Java Errors, So many errors so little time. Short code Please and Thank you!

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. Also write a java program to test your function

MUST use recursion This is my code

public class GCDNO1 {

public static int GCD(int a, int b)

{

if (a mod b == 0)

return b;

else if (a mod b !=0)

return GCD(b, a mod b);

else

return (a-1, GCD(a,b-1));

}

public static void main(String[] args) {

System.out.println("GCD(a,b)= " + GCD(a,b));

}

}

Thank you!

Update:

O wow that was a lot of responses very quickly thank you for all your help. the mod error was a really stupid mistake cant believe I see that. I will go fix the program and come back to award best answer. Thank you again to everyone for your help!

5 Answers

Relevance
  • 9 years ago

    I did not check logic in your problem. Are you using Euclids algorithm to find GCD?

    public class GCDNO1 {

    public static int GCD(int a, int b)

    {

    if (a % b == 0) return b;

    else if (a % b !=0)

    return GCD(b, a % b);

    else

    return GCD(a-1, GCD(a,b-1)); //check this line. I have corrected only compilation errors.

    }

    public static void main(String[] args) {

    System.out.println("GCD(10,70)= " + GCD(10, 70));

    }

    }

  • 9 years ago

    public static int GCD(int a, int b)

    {

    if (a % b == 0)

    return b;

    else

    return GCD(a, b-1);

    }

    //You'll also want to provide values for a and b first in main, example

    int a = Integer.parseInt(args[0]); //from command line

    int b = Integer.parseInt(args[1]);

    System.out.println("GCD(a,b)= " + GCD(a,b)); //or you can just replace a and b with anything

  • Anonymous
    9 years ago

    You need to actually use the mod symbol (%) instead of typing mod! Change the lines that say stuff like "a mod b" to "a % b". Also, your else clause does nothing. You should replace the else if with an else and get rid of the original else because it will never be executed: a % b either equals 0 or it doesn't, there's no other option. You didn't say what errors you are having, so if there are others, you should add more details. I hope this helps.

  • Anonymous
    9 years ago

    public class GCDNO1 {

    public static int GCD(int a, int b)

    {

    if (a % b == 0)

    {

    return b;

    }

    else if (a % b !=0)

    {

    return GCD(b, a % b);

    }

    else

    {

    return GCD((a-1), (GCD(a, b-1)));

    }

    }

    public static void main(String[] args)

    {

    int a = 30;

    int b = 5;

    System.out.println("GCD(a,b)= " + GCD(a, b));

    }

    }

    Source(s): Java skillz
  • How do you think about the answers? You can sign in to vote the answer.
  • 9 years ago

    a mod b is a % b in Java.

Still have questions? Get your answers by asking now.