20 Pattern Programs

20 Pattern Programs in C++ (With Code & Output)

IntermediateTopic: Advanced Pattern Programs
Back

C++ 20 Pattern Programs Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
using namespace std;

int main() {
    int rows = 5;
    
    // Pattern 1: Right Half Pyramid
    cout << "Pattern 1 - Right Half Pyramid:" << endl;
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= i; j++) {
            cout << "* ";
        }
        cout << endl;
    }
    
    // Pattern 2: Left Half Pyramid
    cout << "\nPattern 2 - Left Half Pyramid:" << endl;
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= rows - i; j++) {
            cout << "  ";
        }
        for (int j = 1; j <= i; j++) {
            cout << "* ";
        }
        cout << endl;
    }
    
    // Pattern 3: Full Pyramid
    cout << "\nPattern 3 - Full Pyramid:" << endl;
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= rows - i; j++) {
            cout << " ";
        }
        for (int j = 1; j <= 2 * i - 1; j++) {
            cout << "*";
        }
        cout << endl;
    }
    
    // Pattern 4: Inverted Pyramid
    cout << "\nPattern 4 - Inverted Pyramid:" << endl;
    for (int i = rows; i >= 1; i--) {
        for (int j = 1; j <= rows - i; j++) {
            cout << " ";
        }
        for (int j = 1; j <= 2 * i - 1; j++) {
            cout << "*";
        }
        cout << endl;
    }
    
    // Pattern 5: Diamond
    cout << "\nPattern 5 - Diamond:" << endl;
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= rows - i; j++) {
            cout << " ";
        }
        for (int j = 1; j <= 2 * i - 1; j++) {
            cout << "*";
        }
        cout << endl;
    }
    for (int i = rows - 1; i >= 1; i--) {
        for (int j = 1; j <= rows - i; j++) {
            cout << " ";
        }
        for (int j = 1; j <= 2 * i - 1; j++) {
            cout << "*";
        }
        cout << endl;
    }
    
    return 0;
}
Output
Pattern 1 - Right Half Pyramid:
*
* *
* * *
* * * *
* * * * *

Pattern 2 - Left Half Pyramid:
        *
      * *
    * * *
  * * * *
* * * * *

Pattern 3 - Full Pyramid:
    *
   ***
  *****
 *******
*********

Pattern 4 - Inverted Pyramid:
*********
 *******
  *****
   ***
    *

Pattern 5 - Diamond:
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

Understanding 20 Pattern Programs

This collection includes 20 different pattern programs: various pyramids, triangles, diamonds, squares, rectangles, hollow patterns, number patterns, alphabet patterns, and special shapes. Each pattern demonstrates different loop techniques and logic.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Table of Contents