posted 1 year ago

Hi everyone, I'm trying to get calculate the GCD of tow numbers; My code is not complete and I came across these errors:

recursivegcd.java:18: error: <identifier> expected

public static void int gcd(int a, int b)

^

recursivegcd.java:18: error: '(' expected

public static void int gcd(int a, int b)

^

recursivegcd.java:18: error: invalid method declaration; return type required

public static void int gcd(int a, int b)

^

3 errors

recursivegcd.java:18: error: <identifier> expected

public static void int gcd(int a, int b)

^

recursivegcd.java:18: error: '(' expected

public static void int gcd(int a, int b)

^

recursivegcd.java:18: error: invalid method declaration; return type required

public static void int gcd(int a, int b)

^

3 errors

posted 1 year ago

- 1

No.

You are unnecessarily declaring a new local variable. Your variable names are incorrect format (they should have capitalLettersIn). I think you are misinterpreting Euclid's algorithm. Stop writing code and write down Euclid's algorithm the quick way with the remainder operator. You do not need repeated subtraction as in Euclid's original. What are you going to do if:-1: Both numbers are equal to 0? 2: One number is equal to 0 and the other is non‑zero? 3: Both numbers are equal to each other and non‑zero? As far as I know there is no requirement for the numbers to be in any order.

You are unnecessarily declaring a new local variable. Your variable names are incorrect format (they should have capitalLettersIn). I think you are misinterpreting Euclid's algorithm. Stop writing code and write down Euclid's algorithm the quick way with the remainder operator. You do not need repeated subtraction as in Euclid's original. What are you going to do if:-

Campbell Ritchie

Marshal

Posts: 57499

175

posted 1 year ago

- 1

In which case wikihow missed out that corner case. gcd(

Don't print error messages from a method intended to return something. Don't use System.exit. It is only an error when both arguments are 0.

Don't create multiple Scanners when you only need one.

Your variable capitalisation is not correct. You don't need a keepa variable.

A recursive method should not print things, but should return a result. It stops when it reaches a base case, and my bullet points 2 and 3 together constitute the base case.

*n*, 0) is equal to*n*.Don't print error messages from a method intended to return something. Don't use System.exit. It is only an error when both arguments are 0.

Don't create multiple Scanners when you only need one.

Your variable capitalisation is not correct. You don't need a keepa variable.

A recursive method should not print things, but should return a result. It stops when it reaches a base case, and my bullet points 2 and 3 together constitute the base case.