logoStarPatterns
Back to Patterns

Zero-One Triangle

Pattern Result

1
0 1
1 0 1
0 1 0 1
1 0 1 0 1

Code Implementation

Explanation

The Zero-One Triangle pattern creates a triangle where each position alternates between 0 and 1. The starting value for each row depends on the row number: odd-numbered rows start with 1, and even-numbered rows start with 0. Within each row, the values alternate between 0 and 1. The outer loop iterates from 1 to n, representing each row. The inner loop runs from 1 to the current row number, adding the current value (0 or 1) to the row string and then toggling the value for the next position.