// Helene Martin, CSE 142 // Demonstrates fencepost loops public class Loops { public static void main(String[] args) { printLetters2("Atmosphere"); int factors = countFactors(20); System.out.println(factors); printPrimes(50); } // displays all primes smaller than or equal to max public static void printPrimes(int max) { if(max >= 2) { System.out.print(2); for(int i = 3; i <= max; i++) { if(countFactors(i) == 2) { System.out.print(", " + i); } } } } // returns the count of factors that number has public static int countFactors(int number) { int counter = 0; for(int i = 1; i <= number; i++) { if(number % i == 0) { counter++; } } return counter; } // Print each letter from a word separated by commas // Example of post taken out at end public static void printLetters(String word) { for(int i = 0; i < word.length() - 1; i++) { // word.substring(i, i + 1) would also work System.out.print(word.charAt(i) + ", "); } int last = word.length() - 1; System.out.println(word.charAt(last)); } // Print each letter from a word separated by commas // Example of post taken out at beginning public static void printLetters2(String word) { System.out.print(word.charAt(0)); for(int i = 1; i < word.length(); i++) { // word.substring(i, i + 1) would also work System.out.print(", " + word.charAt(i)); } System.out.println(); } }