/* * This program demonstrates method. */ class PrimesMethod { /* * Returns true iff n is prime. */ static boolean isPrime(int n) { int d = 2; while (d <= n-1) { if (n % d == 0) return false; d++; } return true; } /* * Prints all primes <= limit, ten primes per line, where limit is * the number entered by the user as a command line argument. */ public static void main(String[] args) { int limit = Integer.parseInt(args[0]); int count = 0; int n = 2; while (n <= limit) { if (isPrime(n)) { count++; if (count % 10 == 0) System.out.printf("%5d%n", n); else System.out.printf("%5d", n); } n++; } if (count % 10 != 0) System.out.println(); } }