Skip to content

18. Triangle Character Pattern

Example

Pattern 14

\(N = 5\)

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

void printTriangle(int n) {
    for(int i=1; i<=n; i++) {
        char ch = 'A';
        for(int j=0; j<n; j++) {
            ch++;
        }
        for(int j=0; j<i; j++) {
            cout << --ch << " ";
        }
        cout << endl;
    }
}

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

Input: 5
Output:

E
E D
E D C
E D C B
E D C B A