0
0
CppProgramBeginner · 2 min read

C++ Program to Print Star Pattern

You can print a star pattern in C++ using nested for loops; for example, for (int i = 1; i <= n; ++i) { for (int j = 1; j <= i; ++j) { std::cout << "*"; } std::cout << std::endl; } prints a right-angled triangle of stars.
📋

Examples

Inputn = 3
Output* ** ***
Inputn = 5
Output* ** *** **** *****
Inputn = 1
Output*
🧠

How to Think About It

To print a star pattern, think of rows and columns. For each row from 1 to n, print stars equal to the row number. Use one loop for rows and a nested loop for printing stars in each row.
📐

Algorithm

1
Get the number of rows n from the user
2
For each row i from 1 to n, do:
3
For each column j from 1 to i, print a star
4
After printing stars in a row, move to the next line
5
End
💻

Code

cpp
#include <iostream>

int main() {
    int n;
    std::cout << "Enter number of rows: ";
    std::cin >> n;

    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= i; ++j) {
            std::cout << "*";
        }
        std::cout << std::endl;
    }

    return 0;
}
Output
Enter number of rows: 5 * ** *** **** *****
🔍

Dry Run

Let's trace the program for n = 3 to see how stars are printed.

1

Input

User enters n = 3

2

First row

i = 1, print 1 star: *

3

Second row

i = 2, print 2 stars: **

4

Third row

i = 3, print 3 stars: ***

Row (i)Stars printed
1*
2**
3***
💡

Why This Works

Step 1: Outer loop controls rows

The outer for loop runs from 1 to n, controlling how many rows to print.

Step 2: Inner loop prints stars

The inner for loop runs from 1 to the current row number, printing that many stars.

Step 3: New line after each row

After printing stars for a row, std::cout << std::endl; moves the cursor to the next line.

🔄

Alternative Approaches

Using while loops
cpp
#include <iostream>

int main() {
    int n, i = 1;
    std::cout << "Enter number of rows: ";
    std::cin >> n;

    while (i <= n) {
        int j = 1;
        while (j <= i) {
            std::cout << "*";
            ++j;
        }
        std::cout << std::endl;
        ++i;
    }
    return 0;
}
This uses <code>while</code> loops instead of <code>for</code> loops; it is slightly longer but works the same.
Using recursion
cpp
#include <iostream>

void printStars(int count) {
    if (count == 0) return;
    printStars(count - 1);
    std::cout << "*";
}

void printPattern(int row, int n) {
    if (row > n) return;
    printStars(row);
    std::cout << std::endl;
    printPattern(row + 1, n);
}

int main() {
    int n;
    std::cout << "Enter number of rows: ";
    std::cin >> n;
    printPattern(1, n);
    return 0;
}
This recursive approach is elegant but less intuitive for beginners.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses nested loops: the outer loop runs n times and the inner loop runs up to n times, resulting in O(n^2) time.

Space Complexity

The program uses a fixed amount of extra space for variables, so space complexity is O(1).

Which Approach is Fastest?

The nested for loop approach is straightforward and efficient; recursion adds overhead and is less efficient.

ApproachTimeSpaceBest For
Nested for loopsO(n^2)O(1)Simple and efficient star patterns
While loopsO(n^2)O(1)Alternative loop style, same efficiency
RecursionO(n^2)O(n)Elegant but uses more stack space
💡
Use nested loops where the outer loop controls rows and the inner loop prints stars per row.
⚠️
Beginners often forget to print a new line after each row, causing all stars to print on one line.