title: How does this prime number test in Java work?
body: <p>The code snippet below checks whether a given number is a prime number. Can someone explain to me why this works? This code was on a study guide given to us for a Java exam.</p>
  
  <pre><code>public static void main(String[] args)
  {    
      int j = 2;
      int result = 0;
      int number = 0;
      Scanner reader = new Scanner(System.in);
      System.out.println("Please enter a number: ");
      number = reader.nextInt();
      while (j &lt;= number / 2)
      {
          if (number % j == 0)
          {
             result = 1;
          }
          j++;
      }
      if (result == 1)
      {
          System.out.println("Number: " + number + " is Not Prime.");
      }
      else
      {
          System.out.println("Number: " + number + " is Prime. ");
      }
  }
  </code></pre>