Skip to content

17. Triangle Character Pattern

Example

Pattern 17

\(N = 5\)

\[\Large
\begin{align*}
    &\phantom{A}\phantom{A}\phantom{A}\phantom{A}A \\
    &\phantom{A}\phantom{A}\phantom{A}ABA \\
    &\phantom{A}\phantom{A}ABCBA \\
    &\phantom{A}ABCDCBA \\
    &ABCDEDCBA \\
\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-i; j++) {
            cout << " ";
        }
        for(int j=0; j<i; j++) {
            cout << ch++;
        }
        --ch;
        for(int j=1; j<i; j++) {
            cout << --ch;
        }
        cout << endl;
    }
}

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

Input: 5
Output:

    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA