Simple Triangle/Half Pyramid – Incremental Alphabets (With C++ Code) | Pattern Printing Programs

In this tutorial we will understand how to code a triangle pattern and different variations. It is also known as Half Pyramid Pattern. In the pattern we shall print variations of different letters in an incremental way. Later we will also write a program to print this pattern in C++ Programming Language.

C++ Program for printing 6 different Triangle/Half Pyramid Programs with Incremental Numbers –
#include<iostream>

using namespace std;

void halfPyramidAlphabets1(int n) {
  char alphabet = 'a';
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      cout << alphabet;
    }
    alphabet++;
    cout << endl;

  }
}
void halfPyramidAlphabets2(int n) {
  char alphabet = 'a';
  for (int i = n; i > 0; i--) {
    for (int j = 1; j <= i; j++) {
      cout << alphabet;
    }
    cout << endl;
    alphabet++;
  }
}

void halfPyramidAlphabets3(int n) {
  char alphabet = 'a';
  for (int i = 1; i <= n; i++) {
    for (int k = n - i; k > 0; k--)
      cout << " ";
    for (int j = 1; j <= i; j++) {
      cout << alphabet;
    }
    cout << endl;
    alphabet++;
  }
}

void halfPyramidAlphabets4(int n) {
  char alphabet = 'a';
  for (int i = n; i > 0; i--) {
    for (int k = n - i; k > 0; k--)
      cout << " ";
    for (int j = 1; j <= i; j++) {
      cout << alphabet;
    }
    cout << endl;
    alphabet++;
  }
}
void halfPyramidAlphabets5(int n) {

  for (int i = 1; i <= n; i++) {
    char alphabet = 'a';
    for (int j = 1; j <= i; j++) {
      cout << alphabet++;
    }
    cout << endl;
  }
}

void halfPyramidAlphabets6(int n) {
  char alphabet = 'a';
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {
      cout << alphabet++;
    }
    cout << endl;
  }
}

int main() {
  int num;
  cout << "Enter number of levels of the pattern :" << endl;
  cin >> num;
  halfPyramidAlphabets1(num);
  cout << endl;
  halfPyramidAlphabets2(num);
  cout << endl;
  halfPyramidAlphabets3(num);
  cout << endl;
  halfPyramidAlphabets4(num);
  cout << endl;
  halfPyramidAlphabets5(num);
  cout << endl;
  halfPyramidAlphabets6(num);

  return 0;
}
YouTube video tutorial –

Leave a Reply

Your email address will not be published. Required fields are marked *