Concentric Number Diamond in C++

What You’ll Learn
How to print a full symmetric concentric number pattern in C++ with layers from k down to 1 and back.
Compared to Program 46 (top half), this program adds the bottom half so the output becomes a (2k-1) × (2k-1) pattern.
⭐ 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 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5Complete C++ Program
We print the top half (rows k..1) and then the bottom half (rows 2..k) using the same left/right mirror logic.
#include <iostream>
using namespace std;
int main() {
int i, j;
int k = 5;
// First Part
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";
}
// Second Part
for (i = 2; i <= k; 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
Pick the max value k
k = 5 sets the outer layer and determines width/height (2*k-1).
Top half: i from k down to 1
This gradually decreases the minimum value allowed in the center until it reaches 1.
Mirror each row left and right
We print a descending half (k..1) and then an ascending half (2..k) with the same clamp rule.
Bottom half: i from 2 up to k
This mirrors the top half around the center row to complete the full pattern.
Full concentric layers
The grid size is (2k-1)×(2k-1), so runtime grows like O(k²).
Variation — User Input Version
Let the user choose k and print the full (2k-1) by (2k-1) pattern.
#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";
}
for (int i = 2; i <= k; 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
- Use
setw()if you print multi-digit values - Print without trailing spaces by building each row into a string
- Replace numbers with characters to form layered frames
- Try a distance-based approach (min distance to edge) as an alternative method
- Validate input and handle
cin.fail()
Avoid
- Hard-coding 5 if you want a reusable pattern
- Using tabs for alignment (different consoles render tabs differently)
- Using
endlinside loops (extra flushing) - Printing invalid sizes (0 or negative) without handling
Key Takeaways
Width and height are both 2k-1.
The pattern is symmetric top-to-bottom and left-to-right.
Two parts (k..1 and 2..k) complete the full shape.
Runtime grows like O(k²).
❓ Frequently Asked Questions
i, which becomes 1 at the middle.setw() instead.Explore More C++ Number Patterns!
Patterns like this are a fun way to practice symmetry, loops, and matrix-style thinking.
A common alternative solution computes the value at each cell using the minimum distance to any border. Both approaches produce the same concentric layers.
12 people found this page helpful
