// Helene Martin, CSE 143 // This program demonstrates that the same problem can be // solved iteratively and recursively. public class Stars { public static void main(String[] args) { writeStars(3); writeStars(15); writeStars(0); // edge case } // Prints a line containing the given number of stars. // Precondition: n >= 0 public static void writeStars(int n) { if (n == 0) { // base case (simplest instance of the problem) System.out.println(); } else { // recursive case System.out.print("*"); writeStars(n - 1); } /* It can be useful to write out specific cases * to see the pattern for the recursive case else if (n == 2) { System.out.print("*"); writeStars(1); } else if (n == 3) { System.out.print("*"); writeStars(2); } */ } // Prints a line containing the given number of stars. // Precondition: n >= 0 public static void writeStars2(int n) { for (int i = 0; i < n; i++) { System.out.print("*"); } System.out.println(); } }