title: Is there a more efficient way to write the following Prime Number code?
body: <p>I have the following code which spits out prime numbers between 1 and N. A friend came up with this solution but I believe there is a more efficient way to write this code. Such as making it so that <code>if (i%j!=0) {System.out.print (i + " ");}</code>. However I found this spat out numbers randomly all over the place...</p>
  
  <pre><code>import java.util.Scanner;
  
  public class AllPrime {
  
  
  public static void main(String[] args) {
  
      System.out.println("Enter a number:\n");
      Scanner input = new Scanner(System.in);
      int a = input.nextInt();
  
      for (int i = 2; i &lt; a; i++) {
          boolean primeNum = true;
          for(int j=2; j&lt;i; j++) {
              if (i%j==0) {
                  primeNum =false;
              }
          }
          if (primeNum) {
              System.out.print(i + " ");
              }
          }
      }
  }
  </code></pre>