(Prime integers) Write a Java program that displays the prime numbers between 2 and 1,001, inclusive. Feel free to use, and modify appropriately, the code in Chapter 5 of Liang. Format your output so that ten primes are printed per each line. Separate different numbers by one or more spaces. Consider using printf method.

Respuesta :

public class MyClass {

   public static void main(String args[]) {

       int k = 0;

       for (int i = 2; i<= 1001; i++){

           boolean f = false;

           for (int x = 2; x <= i/2; x++){

               if (i % x == 0){

                   f = true;

                   break;

               }

           }

           if (!f){

               if (k < 10){

                   System.out.print(i+" ");

                   k++;

               }

               else{

                   System.out.println("");

                   k=0;

               }

           }

           

       }

   }

}

I hope this helps!