Skip to content

11. Triangle Digit Pattern

Example

Pattern 11

\(N = 5\)

\[\Large
\begin{align}
&1\\
&0\; 1\\
&1\; 0\; 1\\
&0\; 1\; 0\; 1\\
&1\; 0\; 1\; 0\; 1
\end{align}
\]
printTriangle.cpp
#include <bits/stdc++.h>
using namespace std;

void printTriangle(int n) {
    for(int i=1; i<=n; i++) {
        for(int j=0; j<i; j++) {
            if((i+j)%2 == 0)
                cout << "0 ";
            else
                cout << "1 ";
        }
        cout << endl;
    }
}

int main(void) {
    int n;
    cin >> n;
    printTriangle(n);
    return 0;
}

Input: 5
Output:

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