public class EasyRecursion { public static void main(String[] args) { System.out.println(line(7, '*') + "\n" + line(10, '+') + "\n" + line(19, 'o') ); System.out.println(); System.out.println(triangle(5, '*') + "\n" + triangle(6, '+') + "\n" + triangle(9, 'o') ); System.out.println(); System.out.println( sumIntegersTo(7) + " = " + sigma(7) ); System.out.println( sumIntegersTo(12) + " = " + sigma(12) ); } public static String line(int length, char symbol) { if (length < 0) throw new RuntimeException("Invalid line length"); if (length == 0) return ""; return symbol + line(length - 1, symbol); } public static String triangle(int height, char symbol) { if (height < 0) throw new RuntimeException("Invalid Triangle height"); if (height == 0) return ""; return triangle(height - 1, symbol) + "\n" + line(height, symbol); } public static String sumIntegersTo(int n) { if ( n < 0 ) throw new RuntimeException("Negative Number"); if ( n == 0 ) return ""; if ( n == 1) return "1"; return sumIntegersTo(n - 1) + " + " + n; } public static int sigma(int n) { if (n < 0) throw new RuntimeException("Number must be >= 0"); if ( n == 0 ) return 0; if ( n == 1 ) return 1; return sigma( n-1 ) + n; } }