Concentric Number Pattern in C++

What You’ll Learn
How to print a concentric-style number pattern in C++ where the value decreases as you move toward the center, and then increases again.
For k = 5, each row mirrors around the center to form a sequence like:
5 4 3 2 1 2 3 4 5
⭐ Pattern Output
For k = 5, the pattern looks like this:
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5Complete C++ Program
The left half prints from k down to 1, the right half prints from 2 up to k. For each row, the minimum value allowed is the row index i.
#include <iostream>
using namespace std;
int main() {
int i, j;
int k = 5;
for (i = k; i >= 1; i--) {
for (j = k; j >= 1; j--) {
if (j > i)
cout << j << " ";
else
cout << i << " ";
}
for (j = 2; j <= k; j++) {
if (j > i)
cout << j << " ";
else
cout << i << " ";
}
cout << "\n";
}
return 0;
}🧠 How It Works
Set the max value
k = 5 decides the outer value and also the number of rows.
Row loop chooses i
The outer loop runs i from k down to 1. Smaller i means the center gets smaller.
Left half prints k..1 with a floor
We iterate j from k down to 1. If j > i we print j, otherwise we print i.
Right half mirrors 2..k
The second inner loop prints from 2 up to k using the same rule, which mirrors the row around the center.
Concentric look
The minimum value moves toward the center as the rows progress, creating a layered effect.
Variation — User Input Version
Let the user choose k. The width becomes 2*k-1.
#include <iostream>
using namespace std;
int main() {
int k;
cout << "Enter the value of k: ";
cin >> k;
if (k <= 0) return 0;
for (int i = k; i >= 1; i--) {
for (int j = k; j >= 1; j--) {
cout << ((j > i) ? j : i) << " ";
}
for (int j = 2; j <= k; j++) {
cout << ((j > i) ? j : i) << " ";
}
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Remove the trailing spaces by building each row into a string first
- Print as a full square (height
2*k-1) to make a classic concentric square - Replace numbers with letters to create an alphabet version
- Format with
setw()if you print multi-digit numbers - Validate input with
cin.fail()for robustness
Avoid
- Hard-coding 5 everywhere if you want to scale the pattern
- Mixing spaces and tabs (alignment changes across terminals)
- Using
endlinside loops (extra flushing) - Printing negative or zero sizes without handling
Key Takeaways
Each row is a mirror of descending then ascending values.
The condition j > i decides whether to print j or clamp to i.
Width is 2*k-1 for this half-square version.
Runtime is O(k²) for size k.
❓ Frequently Asked Questions
i = 1, the clamp value is 1, so the descending half reaches 1 at the center and then mirrors back upward." " after each number, but alignment becomes harder to read for multi-digit values.2*k-1 rows and compute each cell based on the minimum distance to the border (classic concentric square approach).k × (2*k-1) values.Explore More C++ Number Patterns!
Mirror logic like this is useful for many symmetric patterns and matrix-style problems.
You can view each row as taking the maximum of the row’s minimum value (i) and the column label (j), then mirroring it around the center.
12 people found this page helpful
