TutorialStudyMite

Pattern Programs in C

GGarvit Gulati5 min read
Beginner friendly

Track completion, mastery, and revision.

Pattern Programs in C++

Pattern printing programs are classic exercises for building strong logical thinking and mastering control flow in C++. These programs rely heavily on nested loops (loops inside other loops).

By learning how to manipulate the loop counters, you can control the number of rows, columns, spaces, and characters printed to the console.


Understanding the Logic Behind Patterns

Most pattern programs use a two-dimensional grid approach:

  1. Outer Loop: Controls the rows (vertical movement).
  2. Inner Loops: Control the columns, spaces, and characters printed within each row (horizontal movement).

💡 Tip: Before writing code, always analyze the pattern on grid paper. Count the number of spaces and characters in each row relative to the current row index (i).


Star Pattern Programs

1. Full Pyramid Star Pattern

This pattern prints a centered pyramid of stars.

    *
   ***
  *****
 *******
*********
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter number of rows to be printed\n";
    cin >> n;
    
    for (int i = 1; i <= n; ++i) { // Loop for each row
        for (int j = 0; j < n - i; ++j) { // Loop for printing spaces
            cout << " ";
        }
        for (int j = 0; j < 2 * i - 1; ++j) { // Loop for printing stars
            cout << "*";
        }
        cout << "\n"; // Move to next line
    }
    return 0;
}

2. Spaced Pyramid Star Pattern

A variation of the pyramid where stars are separated by spaces.

      *
     * *
    * * *
   * * * *
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter number of rows to be printed\n";
    cin >> n;
    
    for (int i = 1; i <= n; ++i) { // Loop for each row
        for (int j = 0; j < n - i; ++j) { // Loop for printing spaces
            cout << " ";
        }
        for (int j = 1; j <= i; ++j) { // Loop for printing star and space
            cout << "* ";
        }
        cout << "\n";
    }
    return 0;
}

3. Hollow Pyramid Star Pattern

Prints only the boundary stars of a pyramid, leaving the inside hollow.

    *
   * *
  *   *
 * * * *
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter number of rows to be printed\n";
    cin >> n;
    
    for (int i = 0; i < n - 1; ++i) { // For rows till second-last
        int j;
        for (j = 1; j <= n + i; ++j) { // Moving cursor across the row
            if (j == n - i || j == n + i)
                cout << "*";
            else
                cout << " ";
        }
        cout << "\n";
    }
    for (int j = 0; j < n; ++j) { // Printing the last row
        cout << "* ";
    }
    cout << "\n";
    return 0;
}

4. Hollow Rectangle Pattern

Prints a hollow rectangular border based on user-defined length and breadth.

* * * * * * *
*           *
*           *
* * * * * * *
#include <iostream>
using namespace std;

int main() {
    int l, b;
    cout << "Enter length and breadth separated by space\n";
    cin >> l >> b;
    
    for (int i = 0; i < b; ++i) { // Loop for rows
        if (i == 0 || i == b - 1) { // First and last row
            for (int j = 0; j < l; ++j) {
                cout << "* ";
            }
        } else { // Middle hollow rows
            for (int j = 0; j < 2 * l; ++j) {
                if (j == 0 || j == 2 * l - 4)
                    cout << "*";
                else
                    cout << " ";
            }
        }
        cout << "\n";
    }
    return 0;
}

5. Inverted Slanted Star Pattern

An inverted right-aligned triangle with increased indentation on each row.

* * * * *
    * * * *
        * * *
            * *
                *
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter number of rows to be printed\n";
    cin >> n;
    
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < 4 * i; ++j) {
            cout << " "; // Printing leading spaces
        }
        for (int j = 0; j < n - i; ++j) {
            cout << "* "; // Printing stars
        }
        cout << "\n";
    }
    return 0;
}

6. Inverted Left-Aligned Triangle

A simple descending star pattern.

* * * * *
* * * *
* * *
* *
*
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter number of rows to be printed\n";
    cin >> n;
    
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n - i; ++j) {
            cout << "* ";
        }
        cout << "\n";
    }
    return 0;
}

7. Right-Pointing Half Diamond

A vertically mirrored triangle aligned to the right.

        *
      * *
    * * *
  * * * *
* * * * *
  * * * *
    * * *
      * *
        *
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter height of triangle\n";
    cin >> n;
    
    // Upper half
    for (int i = 1; i <= n; ++i) {
        for (int j = 0; j < (2 * (n - i)); ++j) {
            cout << " ";
        }
        for (int j = 0; j < i; ++j) {
            cout << "* ";
        }
        cout << "\n";
    }
    // Lower half
    for (int i = n - 1; i > 0; --i) {
        for (int j = 0; j < (2 * (n - i)); ++j) {
            cout << " ";
        }
        for (int j = 0; j < i; ++j) {
            cout << "* ";
        }
        cout << "\n";
    }
    return 0;
}

8. Hourglass Star Pattern

Finished reading?

Was this helpful?

Your feedback shapes better tutorials for everyone.