import java.util.*; public class Print { public static void main(String[] args) { // prints out primes up to a maximum number Scanner console = new Scanner(System.in); System.out.print("Maximum number? "); int max = console.nextInt(); System.out.print(2); int sum = 1; for (int i = 3; i <= max; i++) { if (countFactors(i) == 2) { System.out.print(", " + i); sum++; } } System.out.println(); System.out.println(sum + " total primes"); } // returns the number of factors of MAX public static int countFactors(int max) { int sum = 0; for (int i = 1; i <= max; i++) { if (max % i == 0) { // the following line was for debugging //System.out.println(i); sum++; } } return sum; } // prints a number of powers of a base as // given by the user public static void printPowers() { Scanner console = new Scanner(System.in); System.out.print("Base: "); int base = console.nextInt(); System.out.print("Max exponent: "); int exp = console.nextInt(); System.out.println("The first " + exp + " powers of " + base + " are:"); System.out.print(base); for (int i = 2; i <= exp; i++) { System.out.print(", " + (int)Math.pow(base, i)); } System.out.println(); } // prints the factors of MAX public static void printFactors(int max) { System.out.print("[1"); for (int i = 2; i <= max; i++) { if (max % i == 0) { System.out.print(", " + i); } } System.out.println("]"); } // fencepost solution to printing numbers public static void printNumbers(int max) { System.out.print(1); for (int i = 2; i <= max; i++) { System.out.print(", " + i); } System.out.println(); } // another solution to the fencepost problem public static void printNumbers2(int max) { for (int i = 1; i <= max - 1; i++) { System.out.print(i + ", "); } System.out.print(max); System.out.println(); } // yet another solution to the fencepost problem // that is less efficient, because it has // to do the if test every time public static void printNumbers3(int max) { for (int i = 1; i <= max; i++) { System.out.print(i); if (i != max) { System.out.print(", "); } } System.out.println(); } }