Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

GCD java code

Here is my code.

import java.util.*;
public class GCD
{
public static void main (String args [] )
{
Scanner reader = new Scanner(System.in);
int a, b, x, d = 0, c = 0;
String response;
do
{
System.out.println("Please enter first number to find GCD.");
a = reader.nextInt();
System.out.println("Please enter second number to find GCD.");
b = reader.nextInt();

for (x = a-1; x>=1; x--)
{
c = a%x;
if (c==0)
{
d = b%x;
if (d==c)
{
break;
}
}
}

System.out.println(x);

System.out.println("Would you like to go again?");
response = reader.nextLine();
response = reader.nextLine();
}while ( response.equals("yes") );


System.out.println("Have a nice day.");
}
}


Why doesn't it work. We haven't learned the methods so we have to use loops. Can anyone help me out?
Firstly OP please edit ur post and use the Code Tags whicj makes ur code readable
Knowledge is Power - Be Powerful!!!!!


LIFE IS A CHALLENGE SO ACCEPT IT!
What do you mean it "doesn't work"?

I think you should fix this for some reason...it's like a pet peeve.

Code
while ( response.equals("yes"));
I'd make that ignorecaps...or...

Code
response = reader.nextLine().toLowerCase();

That way instead of
Code
Would you like to go again?
YeS
Have a nice day.
It does...
Code
Would you like to go again?
YeS
Please enter first number to find GCD.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by DeadbyteFeb 13, 2015 09:00 AM 0 108 Feb 13, 2015 09:00 AM
Last Post: Deadbyte
[Java] by President HolyFeb 8, 2014 11:48 AM 3 192 Feb 8, 2014 11:48 AM
Last Post: Knautschtabak
[Java] by CatchMeIfYouCanOct 23, 2012 12:53 PM 6 438 Oct 23, 2012 12:53 PM
Last Post: redpois0n