1 /**
2 This program demonstrates how to print a triangle using recursion.
3 */
4 public class TrianglePrinter
5 {
6 public static void main(String[] args)
7 {
8 printTriangle(4);
9 }
10
11 /**
12 Prints a triangle with a given side length.
13 @param sideLength the length of the bottom row
14 */
15 public static void printTriangle(int sideLength)
16 {
17 if (sideLength < 1) { return; }
18 printTriangle(sideLength - 1);
19
20 // Print the row at the bottom
21
22 for (int i = 0; i < sideLength; i++)
23 {
24 System.out.print("[]");
25 }
26 System.out.println();
27 }
28 }