C++ Star Pattern Programs

Beginner
6 min read
Updated: Nov 2026
3 examples
cout + newline

Definition and Usage

A C++ star pattern is a console program that prints '*' in rows. The outer for loop chooses the row. The inner loop prints each star with cout, then cout << '\n' ends that row.

'*' + 1 is an int. The char is promoted before the addition, so cout << ('*' + 1) writes 43, not another star. string("*") + "*" is the string "**". Two string literals cannot be added: "*" + "*" does not compile.

cout << '*' stays on the row.

It writes the character and does not end the line. Call cout << '\n' once after the inner loop. string(n, '*') builds a solid row of n stars. Include <string> for that constructor.

C++ Syntax

The inner loop runs once per star. cout keeps those stars on one line, and the newline comes after that loop.

C++
for (int row = 1; row <= rows; row++) {
    for (int col = 1; col <= row; col++)
        cout << '*';     // same row
    cout << '\n';       // end the row
}

How C++ Prints a Star

Map each part of the console shape to a C++ value or stream insertion.

PieceC++What it does
Starcout << '*'Writes the character and stays on the row
Run of starsstring(n, '*')A string filled with n stars
Indentstring(n, ' ')n spaces
Char +'*' + 1The int 43
String +string("*") + "*"The string "**"
New rowcout << '\n'Ends the row
Edge onlycout << (col == 1 || col == width ? '*' : ' ')A hollow row

C++ Examples

Three programs. Each one changes only the C++ that chooses how many stars to print, and where the spaces go.

Example 1 — One star per inner-loop pass

Row 1 prints one star. Each later row prints one more, because the inner loop stops at row.

C++
#include <iostream>
using namespace std;

int main() {
    int rows = 5;
    for (int row = 1; row <= rows; row++) {
        for (int col = 1; col <= row; col++)
            cout << '*';
        cout << '\n';
    }
    return 0;
}

How It Works

  1. The outer loop walks row from 1 to 5.
  2. cout << '*' writes the character and stays on the row.
  3. cout << '\n' after the inner loop starts the next row.

Example 2 — Print the whole row with the string constructor

Count row down from 5. One insertion writes every star, and '\n' ends the line.

C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    int rows = 5;
    for (int row = rows; row >= 1; row--)
        cout << string(row, '*') << '\n';
    return 0;
}

How It Works

  1. string(row, '*') fills a std::string with that many stars.
  2. cout writes that string, then '\n' ends the row, so the inner loop is not needed.
  3. row-- makes each later line one star shorter.

Example 3 — Join the spaces and the stars

string(n, ' ') builds the indent. string(n, '*') builds an odd run of stars. + joins the two strings.

C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    int rows = 5;
    for (int row = 1; row <= rows; row++) {
        string spaces(rows - row, ' ');
        string stars(2 * row - 1, '*');
        cout << spaces + stars << '\n';
    }
    return 0;
}

How It Works

  1. Row 1 builds 4 spaces and 1 star. Row 5 builds 0 spaces and 9 stars.
  2. + concatenates two std::strings. '*' + 1 would be the int 43.
  3. cout << '\n' writes the joined line and ends the row.

11 C++ Star Pattern Programs

Each card is a full C++ program: nested for loops, cout << '*' or string(n, '*'), and a newline. Open one for the walkthrough.

Frequently Asked Questions

In C++, '*' + 1 is an int. The char is promoted before the addition, so the sum is 43 and cout writes those digits. cout << '*' writes the character. string("*") + "*" is the string "**". Two string literals cannot be added: "*" + "*" does not compile.
cout << '*' stays on the same row. cout << '\n' ends the row. Call it once after the inner loop. If the newline sits inside the inner loop, each star lands on its own line.
Yes, when the row is a solid run of stars. cout << string(row, '*') << '\n' writes that row and the newline in one statement. Include <string>. Use the inner loop when the row mixes spaces and stars, or when only the edges should be stars.
cout << string(rows - row, ' ') + string(2 * row - 1, '*') << '\n' builds the indent and the stars, then ends the row. Row 1 has one star; each later row adds two.

Next: Star Pattern 1

A full C++ walkthrough of the right-angled star triangle, from cout << '*' through the newline.

Star Pattern 1 →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

28 people found this page helpful