class PrimesUsingMethods { /** * Checks whether its parameter is a prime or not. * @param n a positive integer * @return true if n is a prime, false otherwise */ static boolean isPrime(int n) { for (int d = 2; d < n; d++) if (n % d == 0) return false; return true; } /** * Prints all primes <= maxN, 10 primes per line. * @param maxN an integer */ static void printPrimesUpTo(int maxN) { int count = 0; System.out.println("Primes Table\n============"); for (int n = 2; n <= maxN; n++) { if (isPrime(n)) { System.out.printf("%5d", n); count++; if (count == 10) { System.out.println(); count = 0; } } } if (count < 10) System.out.println(); } /* * This program gets an integer n from the command line * argument then prints all primes <= n. */ public static void main(String[] args) { printPrimesUpTo(Integer.parseInt(args[0])); } }