title: Why does my code of computing Greatest Common Divisor of multiple integers return nothing?
body: <p>I wrote the following codes to calculate gcd of unspecified number of integers.</p>
  <pre><code>import java.util.Scanner;
  public class GcdOfUnspecifiedNumberOfIntegers {
  
  public static void main(String[] args) {
      
      Scanner input = new Scanner(System.in);
      int[] list = new int[5];
      
      for (int i = 0; i &lt; 5; i++) {
          System.out.println(&quot;Enter a number: &quot;);
          list[i] = input.nextInt();
      }
      
      gcd(list);
  }
  
  // Get the gcd of multiple integers
  public static void gcd(int...numbers) {
      int gcd = 1;
      
      for (int i = 0; i &lt; numbers.length; i++) {
          gcd = gcdOfTwo(gcd, numbers[i]);
      }
      
  System.out.println(&quot;The gcd is: &quot; + gcd);
  
  }
  
  // Get the gcd of two integers
  public static int gcdOfTwo(int a, int b) {
      int gcdOfTwo = 1;
      int possibleGcd = 1;
      
      while (gcdOfTwo &lt;= a &amp;&amp; gcdOfTwo &lt;= b) {
          if (a % possibleGcd == 0 &amp;&amp; b % possibleGcd == 0) {
              gcdOfTwo = possibleGcd;
              possibleGcd++;
          }
      }
      
      return gcdOfTwo;
  }
  
  }
  </code></pre>
  <p>My thought is to get the gcd of two numbers (say it's GCD), and get the gcd of GCD and the third number, and so on and so forth.</p>
  <p>The problem looks like &quot;gcd(list)&quot; isn't working correctly. After I enter all the integers, and press enter key, nothing happens.</p>
  <p>--- update ---</p>
  <p>I've moved possibleGcd++ out of if statement, but it still has something wrong as the console gave me this:</p>
  <pre><code>Enter a number: 
  1
  Enter a number: 
  2
  Enter a number: 
  3
  Enter a number: 
  4
  Enter a number: 
  5
  Exception in thread &quot;main&quot; java.lang.ArithmeticException: / by zero
  at GcdOfUnspecifiedNumberOfIntegers.gcdOfTwo(GcdOfUnspecifiedNumberOfIntegers.java:35)
  at GcdOfUnspecifiedNumberOfIntegers.gcd(GcdOfUnspecifiedNumberOfIntegers.java:22)
  at GcdOfUnspecifiedNumberOfIntegers.main(GcdOfUnspecifiedNumberOfIntegers.java:14)
  </code></pre>
  <hr />
  <h3>Solved</h3>
  <p>I should use while (possibleGcd &lt;= a &amp;&amp; possibleGcd &lt;= b) instead of while (gcdOfTwo &lt;= a &amp;&amp; gcdOfTwo &lt;= b).</p>