// Helene Martin, CSE 142 // Demonstrates several common for loop patterns including cumulative sum // and fencepost. public class Loops { public static void main(String[] args) { printConsonants("go seahawks"); printLetters("CHAMPIONS"); System.out.println(countFactors(20)); printPrimes(50); } // prints non-vowel letters from a given phrase // e.g.: vowels -> vwls public static void printConsonants(String phrase) { for (int i = 0; i < phrase.length(); i++) { char letter = phrase.charAt(i); // letter == 'c' || letter == 'd' || letter == 'b' if (letter != 'a' && letter != 'e' && letter != 'i' && letter != 'o' && letter != 'u') { System.out.print(letter); } } System.out.println(); } // prints letters from a given phrase separated by commas // e.g.: vowels -> v, o, w, e, l, s public static void printLetters(String phrase) { System.out.print(phrase.charAt(0)); for (int i = 1; i < phrase.length(); i++) { System.out.print(", " + phrase.charAt(i)); } System.out.println(); } // counts and returns the factors of a given number public static int countFactors(int num) { int factors = 0; for (int i = 1; i <= num; i++) { if (num % i == 0) { factors++; } } return factors; } // prints all primes up to and including the max public static void printPrimes(int max) { System.out.print(2); for (int i = 3; i <= max; i++) { int factors = countFactors(i); if (factors == 2) { System.out.print(", " + i); } } } }