/** * Title: Chapter 3, "Control Statements" * Description: Chapter 3 Examples * Copyright: Copyright (c) 2000 * Company: Armstrong Atlantic State University * @author Y. Daniel Liang * @version 1.0 */ // PrimeNumber.java: Print first 50 prime numbers // package chapter3; public class PrimeNumber { /**Main method*/ public static void main(String[] args) { int count = 1; // Count the number of prime numbers int number = 2; // A number to be tested for primeness boolean isPrime = true; // Is the current number is prime? System.out.println("The first 50 prime numbers are \n"); // Repeatedly test if a new number is prime while (count <= 50) { // Assume the number is prime isPrime = true; // Set isPrime to false, if the number is not prime for (int divisor = 2; divisor <= number/2; divisor++) { if (number % divisor == 0) //If true, the number is not prime { isPrime = false; break; // Exit the for loop } } // 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 System.out.print(number + " "); count++; // Increase the count } // Check if the next number is prime number++; } } }