The Artima Developer Community
Sponsored Link

Java Answers Forum
Greatest Common Divisor Program Help

3 replies on 1 page. Most recent reply: May 4, 2004 1:57 PM by twc

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Meth

Posts: 19
Nickname: meth
Registered: Mar, 2004

Greatest Common Divisor Program Help Posted: May 2, 2004 5:55 PM
Reply to this message Reply
Advertisement
yet one more program im a lil confused on
here is the specifications

The user enters two positive integers.


Then, the application computes the greatest common divisor of the two numbers and displays all three values.


Then, the user can continue the application or exit.

The formula for finding the greatest common divisor of two positive integers x and y follows the Euclidean algorithm as follows:
1. Subtract x from y repeatedly until y < x.
2. Swap the values of x and y.
3. Repeat steps 1 and 2 until x = 0.
4. y is the greatest common divisor of the two numbers.

• This application can use a while loop for step 1 nested within a do while loop that checks step 3. To swap values, you can use the following technique:
int temp = x;
x = y;
y = temp;

Code should handle invalid data. Use a method to validate integer values.

Also display error message for entry of negative number for first and second value.

Also display error message for entry of zero value for first and second value



Heres my code thus far,


package program5;
import java.io.*;
 
 
/**
* Benjamin Shively
* Java Programming I
* Java Program #5
* Due Date: May 7, 2004
* Computers the greatest common divisor of of two numbers
* and displays all tree numbers
 */
 
public class Gcd {
  public static void main(String[] args) {
 int result = 0;
 BufferedReader myInput = new BufferedReader
    (new InputStreamReader (System.in));
 
try {
String choice = " ";
while(!(choice.equalsIgnoreCase("x"))) {
System.out.print("Enter first positive integer:");
int x = Integer.parseInt(myInput.readLine());
System.out.print("Enter second positive integer:");
int y = Integer.parseInt(myInput.readLine());
do {
        while(y > x) {
        y = y - x;
        }
        }
        while(x != 0);
System.out.println("The greatest common divisor of: \t" + x + "\tand\t" + y + "\tis\t" + y);
System.out.println("Press Enter to Continue or 'x' To Exit:"); 
choice = myInput.readLine();
}
}
catch (Exception e) {}
}
}


Thx for any help in advance


twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Greatest Common Divisor Program Help Posted: May 2, 2004 9:17 PM
Reply to this message Reply
Your main problem is that you have ignored the instructions concerning swapping x and y.

do {
        while(y > x) {
                y = y - x;
        }
        //you need to swap x and y here.  See the directions.
}while(x != 0);


I assume that you realize that you haven't done the validation yet. You are wise to get the other part of the problem solved first.

But when you get there, you should be able to create one (static) method that works for both inputs. You might consider using a parameter to determine whether to print "first" or "second" in the prompt. You could also move the try-catch block into that method.

Just a few thoughts. Get the other stuff working first.

cheers
twc

Meth

Posts: 19
Nickname: meth
Registered: Mar, 2004

Re: Greatest Common Divisor Program Help Posted: May 4, 2004 1:39 PM
Reply to this message Reply
Heres my code so far the problems im having our one the calculations are fine,


however when they print to the consol im getting the divisor in all three spots and what i need it to do is show the orginal two entries and the divisor

Alos i havent done the validation coding yet but besides i think im fine

package program5;
import java.io.*;
 
 
/**
* 
* Java Programming I
* Java Program #5
* Due Date: May 7, 2004
* Computers the greatest common divisor based on user input of two
* numbers and displays all tree numbers to user
 */
 
public class Gcd {
  public static void main(String[] args) {
int z = 0;
 BufferedReader myInput = new BufferedReader
    (new InputStreamReader (System.in));
try {
String choice = " ";
while(!(choice.equalsIgnoreCase("x"))) {
System.out.print("Enter first positive integer:");
int x = Integer.parseInt(myInput.readLine());
System.out.print("Enter second positive integer:");
int y = Integer.parseInt(myInput.readLine());
if (x <= 0 || y <= 0) { 
    System.out.println("\nERROR.Positive Integers Only\n");
System.exit(0);
}
else {
do {
     while(y > x) {
      y = y - x;
      }
int result = y;
y = x;
x = result;
}
while(x != 0 && y > x);
}
System.out.println("The greatest common divisor of:\t" + x + "\tand\t" + y + "\tis\t" + y);
System.out.println("Press Enter to Continue or 'x' To Exit:");
choice = myInput.readLine();
}
}
catch (Exception e) {}
}
}


Thx in advance for any and all help

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Greatest Common Divisor Program Help Posted: May 4, 2004 1:57 PM
Reply to this message Reply
> System.out.println("The greatest common divisor of:\t" + x + "\tand\t" + y + "\tis\t" + y);

Look at your code. You are printing x once and y twice. If the algorithm is working, one of those should be equal to the divisor, and the other should be zero. Actually, I would have thought that your output would be zero, and the divisor twice.

Your problem is caused by the fact that the values of x and y are being changed while the program figures out what the common divisor is. By the time that you print out the results, the original values are long gone.

There are two ways to fix this. One way is to create two more int variables to store the original values. That way after x and y have been modified by your algorithm, you would still have the original x and y values.

A better way would be to move the code that finds the common divisor into a method and use x and y as parameters. That way they never get modified in the first place. The method would work only on copies of x and y. I would also make the parameter names different for clarity. Here is an idea of what I mean.
int comDiv = getCommonDivisor(x, y);
System.out.println("The greatest common divisor of:\t" + x + "\tand\t" + y + "\tis\t" + comDiv);
//elsewhere in your program - NOT in the main method
public static int getCommonDivisor(int a, int b){
// put all of your code that finds the divisor here (don't forget to change variable names to a and b!!!!)
}


I hope this helps.

Flat View: This topic has 3 replies on 1 page
Topic: DLL Previous Topic   Next Topic Topic: Please help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use