Skip to content
C++ Better Explained
Go back
Star Pattern Programs in C++: Triangles, Pyramids, and Diamonds

Star Pattern Programs in C++

Printing star patterns is a rite of passage for every C++ beginner. They look impressive, but they all come down to one idea: nested loops — a loop inside a loop. Once that clicks, you can draw almost any shape on the console.


The Core Idea: Rows and Columns

Every pattern is a grid. The outer loop handles the rows (top to bottom), and the inner loop handles the columns (left to right within a row). After each row, you print a newline to drop to the next line.

#include <iostream>

int main() {
    int rows = 5;
    for (int i = 1; i <= rows; ++i) {      // outer: one pass per row
        for (int j = 1; j <= i; ++j) {     // inner: print i stars
            std::cout << "* ";
        }
        std::cout << "\n";                 // move to the next row
    }
    return 0;
}

Output:

*
* *
* * *
* * * *
* * * * *

The trick is the inner loop condition j <= i. On row 1, i is 1 so you print one star; on row 5, you print five. The number of stars depends on the current row, which is exactly why the loops are nested.


Inverted Right Triangle

Flip the logic: start with the most stars and print one fewer each row. Just have the inner loop count down from rows - i + 1:

#include <iostream>

int main() {
    int rows = 5;
    for (int i = 1; i <= rows; ++i) {
        for (int j = i; j <= rows; ++j) {  // fewer stars as i grows
            std::cout << "* ";
        }
        std::cout << "\n";
    }
    return 0;
}

Output:

* * * * *
* * * *
* * *
* *
*
If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

Centered Pyramid

A pyramid is a triangle plus leading spaces. Each row needs two inner loops: one to print the spaces that push the row toward the center, and one to print the stars. As rows widen, the spaces shrink.

#include <iostream>

int main() {
    int rows = 5;
    for (int i = 1; i <= rows; ++i) {
        for (int s = 1; s <= rows - i; ++s)  // leading spaces
            std::cout << " ";
        for (int j = 1; j <= 2 * i - 1; ++j) // odd number of stars
            std::cout << "*";
        std::cout << "\n";
    }
    return 0;
}

Output:

    *
   ***
  *****
 *******
*********

Two details make this work. First, the spaces per row are rows - i, so they decrease as you go down. Second, the stars per row follow 2 * i - 1, giving the odd counts (1, 3, 5, 7, 9) that keep the pyramid symmetrical.


Diamond Pattern

A diamond is just a pyramid stacked on top of an inverted pyramid. Print the top half, then loop the bottom half in reverse:

#include <iostream>

int main() {
    int n = 4;
    // Top half (including the widest row)
    for (int i = 1; i <= n; ++i) {
        for (int s = 1; s <= n - i; ++s) std::cout << " ";
        for (int j = 1; j <= 2 * i - 1; ++j) std::cout << "*";
        std::cout << "\n";
    }
    // Bottom half
    for (int i = n - 1; i >= 1; --i) {
        for (int s = 1; s <= n - i; ++s) std::cout << " ";
        for (int j = 1; j <= 2 * i - 1; ++j) std::cout << "*";
        std::cout << "\n";
    }
    return 0;
}

Output:

   *
  ***
 *****
*******
 *****
  ***
   *

The only difference between the two halves is the direction of the outer loop: the first counts up, the second counts down.


How to Approach Any Pattern

When you meet a new pattern, ask three questions:

  1. How many rows? That sets your outer loop.
  2. How many characters in each row, and does it depend on the row number? That sets your inner loop condition.
  3. Are there leading spaces? If the shape is centered or right-aligned, add a spaces loop before the stars.

Answer those and the code almost writes itself. Every pattern in this article uses the exact same skeleton — only the inner loop bounds change.



Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Share this post on:

Written by

Sahil Bora

Software Engineer. Author and creator of C++ Better Explained.


Previous Post
Reverse a Number in C++: Flip the Digits with a While Loop
Next Post
std::array in C++: A Safer, Modern Alternative to C-Style Arrays

Keep Learning