programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Debugging the java program to print the GCD of two positive integers using Euclid's algorithm

Khusbu Sinha
Ranch Hand
Posts: 117
1
I am writing the code for a java program that takes two positive integers as user inputs and prints their GCD. My code runs only for those integers one of which is the multiple of the other and not for random integers. Eg 1: Input = 24 4 Output =4
Eg 2: Input 153 91 Output = NA (IDE freezes)
Kindly review my code and help me debug it. Thanks.
Here is my code:

Campbell Ritchie
Marshal
Posts: 56598
172
• 1
No, it doesn't freeze. It fails to terminate. Don't try to mix recursion and loops. Use one or the other. Have a careful look at the loop and work out when it will terminate.

Knute Snortum
Sheriff
Posts: 4288
127
• 1
Two things: First, check your algorithm for calculating the greatest common divisor (GCD).  I'm not sure it's correct.  Second, a recursive method usually starts with a line that will stop the recursion.

Here's another big hint: should a recursive method have a loop?

Khusbu Sinha
Ranch Hand
Posts: 117
1
I debugged my code using conditional if-else statement inside my recursion method and it successfully terminated.

Here is the modified code:

Khusbu Sinha
Ranch Hand
Posts: 117
1
Knute Snortum wrote:a recursive method usually starts with a line that will stop the recursion.

This solved the problem. Thanks.

Knute Snortum
Sheriff
Posts: 4288
127
Good job.  Recursion is a difficult concept.

Campbell Ritchie
Marshal
Posts: 56598
172
Khusbu Sinha wrote:I debugged my code using conditional if-else statement inside my recursion method and it successfully terminated.

Here is the modified code: . . .
Why does line 25 not use the value returned from the GCD method?
Style thing: only use _s in names of constants or package names. The method shou‍ld be called calculateGcd() or calculateGCD(). Or even gcd().

Henry Wong
author
Sheriff
Posts: 23295
125
Khusbu Sinha wrote:I debugged my code using conditional if-else statement inside my recursion method and it successfully terminated.

Here is the modified code:

Thanks for following up -- and posting your answer. You earned a cow.

Henry

Campbell Ritchie
Marshal
Posts: 56598
172
• 1
Surely you can simplify that code because you don't need both the if and the else

Khusbu Sinha
Ranch Hand
Posts: 117
1
Campbell Ritchie wrote:
]Why does line 25 not use the value returned from the GCD method?
.

Because the value returned from the GCD method is the final value and needs no further calculation. So, with'return', it exits the method and never again enters the if else block.

I agree there was no need for both the if and else blocks. Thanks for the simplified code.

Khusbu Sinha
Ranch Hand
Posts: 117
1
@Henry thanks for awarding me with a cow. Its my 1st cow and I feel honored.

 It is sorta covered in the JavaRanch Style Guide.