Bidirectional Number Triangle in C++

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print the number pattern 11111, 2222, 333, 22, 1 in C++ using nested for loops.

The length of each row decreases by 1, while the printed digit follows a mirror rule: 1, 2, 3, 2, 1 for rows = 5.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
11111
2222
333
22
1
1

Complete C++ Program

The inner loop controls the shrinking row length; the printed value is mirrored after the middle row.

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

int main() {
    int rows = 5;
    int i, j;

    for (i = 1; i <= rows; i++) {
        int mid = (rows + 1) / 2;
        int val = (i <= mid) ? i : (rows + 1 - i);

        for (j = i; j <= rows; j++) {
            cout << val;
        }
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Choose the row count

int rows = 5; sets the height and the maximum width.

Setup
2

Outer loop (row index)

for (i = 1; i <= rows; i++) iterates through each line of the pattern.

Row control
3

Compute the mirrored digit

The middle row is mid = (rows + 1) / 2. The value is (i <= mid) ? i : (rows + 1 - i), giving 1, 2, 3, 2, 1 for 5 rows.

Value rule
4

Inner loop (repeat the digit)

for (j = i; j <= rows; j++) prints the digit val exactly rows - i + 1 times.

Repetition
=

Mirrored repeating digits

Total printed digits are n(n+1)/2, so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Let the user decide the number of rows using cin:

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

int main() {
    int rows;
    int i, j;

    cout << "Enter the number of rows: ";
    cin >> rows;

    int mid = (rows + 1) / 2;

    for (i = 1; i <= rows; i++) {
        int val = (i <= mid) ? i : (rows + 1 - i);

        for (j = i; j <= rows; j++) {
            cout << val;
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Validate input (e.g., check cin.fail()) before using rows
  • Print spaces between digits with cout << val << ' '
  • Make it right-aligned by printing leading spaces before each row
  • Use the same logic to build alphabet mirror patterns (A, BB, CCC, ...)
  • For an ascending-width version, flip the inner loop bounds

Avoid

  • Hard-coding 5 everywhere (use rows instead)
  • Forgetting the newline after each row
  • Recomputing constants unnecessarily inside tight loops
  • Using endl in performance-sensitive loop prints

Key Takeaways

1

The inner loop bound (j = i..rows) makes the row length shrink each line.

2

A simple mirror rule produces 1, 2, 3, 2, 1 for 5 rows.

3

Total printed digits are n(n+1)/2, so the work is O(n²).

4

The same idea scales to other patterns by changing only what you print inside the inner loop.

❓ Frequently Asked Questions

Use a mirrored value: with mid = (rows + 1) / 2, compute val = (i <= mid) ? i : (rows + 1 - i).
Because the inner loop runs from j = i to j = rows. That prints exactly rows - i + 1 characters on row i.
Yes. With even rows (like 4), mid becomes 2, and the values mirror as 1, 2, 2, 1 while the row lengths still shrink.
O(n²) for n rows: total printed digits are 1+2+…+n = n(n+1)/2.

Explore More C++ Number Patterns!

Practice nested loops by combining shrinking rows with a simple mirrored value rule.

All Number Patterns →
Did you know?

This pattern mixes two ideas: the triangular row-length count (n+(n-1)+…+1) and a mirrored value mapping that turns a simple row index into 1,2,3,2,1.

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.

12 people found this page helpful