Skip to content

2. Triangle Star Pattern

Example

Pattern 2

\(N = 5\)

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

void printTriangle(int n) {
    for(int i=0; i<n; i++) {
        for(int j=0; j<=i; j++) {
            cout << "* ";
        }
        cout << endl;
    }
}

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

Input: 5
Output:

* 
* * 
* * * 
* * * * 
* * * * *