• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good job.  Recursion is a difficult concept.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely you can simplify that code because you don't need both the if and the else
 
Khusbu Sinha
Ranch Hand
Posts: 117
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!