This little code snippet comes directly from a 'code challenge' I completed on and I found it really interesting so decided to put it up here.

The challenge was to create a binary triangle of a size determined by the user. So the program collected user input for the number of rows and then printed a binary triangle to the console output in the format given in the challenge spec below. The method shown here does not include gathering the user input.

If user gives input 5 then the binary triangle should be as is shown at the top of the code below:

The first loop counts the number of lines that are being printed, up to the value of the input.

The second loop uses the count from the first loop to dictate the number of writes required for that given line. ie, as the counter for the first loop increases, the number of characters printed on each line increases proportionally.

The if-else block prints 1 if the last int was 0, and 0 if the last int was 1, then sets the lastInt value to the appropriate value for the next loop.