title: Problems creating greatest common divisor in java
body: <p>I'm following the logic that I was given for this calculation, but it seems to only give me an endless loop. Can I get some help? </p>
  
  <p>The logic I was supposed to use is : </p>
  
  <p>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 &lt; 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.</p>
  
  <p>Here is my code: </p>
  
  <pre><code>/*
   * To change this license header, choose License Headers in Project Properties.
   * To change this template file, choose Tools | Templates
   * and open the template in the editor.
   */
  
  package project4.pkg3;
  
  /**
   *
   * @author LMFS
   */
  
  import java.util.Scanner;
  
  public class Project43 {
  
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
          // TODO code application logic here
          int numOne, numTwo;
          String c = "";
          Scanner sc = new Scanner(System.in);
          String another = "y";
          while (another.equalsIgnoreCase("y")) {
              System.out.println("Enter first number: ");
              numOne = sc.nextInt();
              System.out.println("Enter second number: ");
              numTwo = sc.nextInt();
  
              c = doMath(numOne, numTwo);
  
              //use static method here.
              System.out.println("Greatest common divisor: " + c);
              System.out.println();
              System.out.println("Continue? (y/n): ");
              another = sc.next();
          }
  
      }
  
      public static String doMath(int numOne, int numTwo) {
          while (numOne != 0) {
              System.out.println("//debugging line 48: " + numOne);
              while (numTwo &gt; numOne) {
                  numTwo -= numOne;
                  System.out.println("//debugging line 52: " + numTwo);
              }
              while (numOne &gt; numTwo) {
                  numOne -= numTwo;
                  System.out.println("//debugging line 57: " + numOne);
              }
          }
          return Integer.toString(numTwo);
      }
  }
  </code></pre>
  
  <p>EDIT: this is solved. I changed the static method to this: </p>
  
  <pre><code>    public static int egcd(int a, int b) {
  if (a == 0)
      return b;
  
  while (b != 0) {
      if (a &gt; b)
          a = a - b;
      else
          b = b - a;
      }
  
  return a;
  }
  </code></pre>