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

This is what comes out after running the code:

This is not wanted I wanted what should I do?

It should be:

Enter first number(must be larger than the second):18

Enter second number:12

The GCD of 12 and 6 is 6The GCD of 18 and 12 is 0

This is not wanted I wanted what should I do?

It should be:

The GCD of 18 and 12 is 6

Campbell Ritchie

Marshal

Posts: 55793

164

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: 55793

164

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.