Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟡 Control Flow  ·  Lesson 19

Pattern Programs in C++ (Nested Loops)

In this lesson: Nested loops and pattern programs in C++ — stars, numbers, pyramids — with code and output. In Hindi & English.

What are nested loops?

A nested loop is simply a loop inside another loop. For every single pass of the outer loop, the inner loop runs all the way through. This two-level counting is exactly what you need to print patterns.

Thinking in rows and columns

The trick to every pattern is this: the outer loop is the row, and the inner loop is the column (what to print across that row). Decide how many rows you want, then decide what each row should contain.

A square of stars

Each of 3 rows prints 3 stars:

C++
for (int i = 1; i <= 3; i++) {       // rows
    for (int j = 1; j <= 3; j++) {   // columns
        cout << "* ";
    }
    cout << "\n";                     // new line after each row
}
Dry Run — Nested loop pattern (i=rows, j=columns), n=3
i (row)Andar j chalta haiPrint hota hai
1j=1,2,3* * *
2j=1,2,3* * *
3j=1,2,3* * *
💡 Aise samajhiye

Nested loop me bahar wala loop (i) rows banata hai, andar wala (j) us row me columns. Har baar bahar wala ek row aage badhta hai, andar wala poora chal ke us row ke saare star print karta hai.

Output:
* * *
* * *
* * *

A right triangle

Make the inner loop depend on the row number, so row i prints i stars:

C++
for (int i = 1; i <= 4; i++) {
    for (int j = 1; j <= i; j++) {   // note: j <= i
        cout << "* ";
    }
    cout << "\n";
}
Output:
*
* *
* * *
* * * *

A number triangle

Print the column number instead of a star:

C++
for (int i = 1; i <= 4; i++) {
    for (int j = 1; j <= i; j++) {
        cout << j << " ";
    }
    cout << "\n";
}
Output:
1
1 2
1 2 3
1 2 3 4

A pyramid

A pyramid adds a spaces loop before the stars — spaces shrink as stars grow:

C++
int rows = 4;
for (int i = 1; i <= rows; i++) {
    for (int s = 1; s <= rows - i; s++) cout << " ";   // spaces
    for (int j = 1; j <= 2 * i - 1; j++) cout << "*";  // stars
    cout << "\n";
}
Output:
   *
  ***
 *****
*******

Common mistakes

  • Forgetting the cout << "\n"; after each row, so everything prints on one line.
  • Using j <= 3 (fixed) when you meant j <= i (grows with the row).
  • Getting the spaces count wrong in a pyramid — it is rows - i.
  • Reusing the same variable name for both loops, breaking the counting.
🏋️ Practice

Print an inverted right triangle (4 stars on top, 1 at the bottom). Then try a number pyramid. Change one loop bound at a time and watch how the shape changes.

Summary

  • A nested loop runs the inner loop fully for each outer pass.
  • For patterns: outer loop = rows, inner loop = what fills each row.
  • Make the inner bound depend on i to get triangles.
  • Pyramids add a spaces loop before the stars.
  • Always print a newline at the end of each row.

Frequently Asked Questions

What is a nested loop in C++?
A nested loop is a loop placed inside another loop. The inner loop completes all its passes for each single pass of the outer loop, which is why nested loops are used for grids, tables and patterns where you need two levels of counting.
How do you print patterns in C++?
Patterns are printed using nested loops: an outer loop controls the rows and one or more inner loops control what is printed in each row. By adjusting how many stars, numbers or spaces the inner loop prints per row, you can build triangles, pyramids and many other shapes.
Why are nested loops used for pattern programs?
Patterns have two dimensions — rows, and the items within each row — so you need two levels of looping. The outer loop moves down the rows while the inner loop fills each row, which maps naturally to the grid-like shape of most patterns.
How do you print a pyramid pattern in C++?
A pyramid needs each row to print some leading spaces followed by stars, with the spaces decreasing and the stars increasing as you go down. You use one inner loop for the spaces and another for the stars, both based on the current row number.
What is the time complexity of a nested loop?
If an outer loop runs n times and the inner loop runs n times for each, the body runs about n × n times, which is O(n²). This is fine for small patterns but grows quickly, so deeply nested loops over large sizes can become slow.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicbreak and continue Statements Your progress: 19 / 81 (23%) · Lesson 20 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free