Bidirectional Number Triangle in C++

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:
11111
2222
333
22
1Complete C++ Program
The inner loop controls the shrinking row length; the printed value is mirrored after the middle row.
#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
Choose the row count
int rows = 5; sets the height and the maximum width.
Outer loop (row index)
for (i = 1; i <= rows; i++) iterates through each line of the pattern.
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.
Inner loop (repeat the digit)
for (j = i; j <= rows; j++) prints the digit val exactly rows - i + 1 times.
Mirrored repeating digits
Total printed digits are n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Version
Let the user decide the number of rows using cin:
#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 usingrows - 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
5everywhere (userowsinstead) - Forgetting the newline after each row
- Recomputing constants unnecessarily inside tight loops
- Using
endlin performance-sensitive loop prints
Key Takeaways
The inner loop bound (j = i..rows) makes the row length shrink each line.
A simple mirror rule produces 1, 2, 3, 2, 1 for 5 rows.
Total printed digits are n(n+1)/2, so the work is O(n²).
The same idea scales to other patterns by changing only what you print inside the inner loop.
❓ Frequently Asked Questions
mid = (rows + 1) / 2, compute val = (i <= mid) ? i : (rows + 1 - i).j = i to j = rows. That prints exactly rows - i + 1 characters on row i.rows (like 4), mid becomes 2, and the values mirror as 1, 2, 2, 1 while the row lengths still shrink.Explore More C++ Number Patterns!
Practice nested loops by combining shrinking rows with a simple mirrored value rule.
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.
12 people found this page helpful
