Skip to content

7. Triangle Star Pattern

Example

Pattern 7

\(N = 5\)

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

void printTriangle(int n) {
    for(int i=1; i<=n; i++) {
        for(int k=0; k<(n-i); k++) {
            cout << " ";
        }
        for(int col=0; col<2*row-1; col++) {
            cout << "*";
        }
        cout << endl;
    }
}

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

Input: 5
Output:

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