// PrimeNumber.java: Print first 50 prime numbers public class primeNumbers { /** Main method */ public static void main(String[] args) { final int NUM_OF_PRIMES = 50; // the number of primes we are seeking int count = 1; // Count the number of prime numbers we have found int number = 2; // A number to be tested for primeness boolean isPrime = true; // Is the current number is prime? This is a flag. System.out.println("The first 50 prime numbers are \n"); // Repeatedly test if a new number is prime while (count <= NUM_OF_PRIMES) { // Assume the number is prime isPrime = true; // Set isPrime to false, if the number is not prime, // i.e. if it divides evenly by the divisor int divisor = 2; // then start up a while loop on this specific number to see if it is a prime // no need to test higher than half of the number being tested while (divisor <= (number/2)) { //If this is true, the number is not prime if (number % divisor == 0) { isPrime = false; break; // This break will Exit the while loop that starts: while (divisor...) } divisor++; // otherwise go ahead and try the next divisor } // end of while loop that starts: while (divisor ...) // Print the prime number and increase the count if (isPrime) { if (count % 10 == 0) { // Print the number and advance to the new line System.out.println(number); } else // just print the number along the same line System.out.print(number + " "); // end of "if" statement: if (count % 10 ... count++; // Increase the count on prime numbers found; once 50 are found, stop. } // end of if statement: if (isPrime) ... // go to the next number and loop again to Check if the next number is prime number++; } // end of the while loop that starts: while (count ...) } // end of method main } // end of class PrimeNumbers