Concentric Number Diamond in C++

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

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:

Output
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 5
1

Complete 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.

C++
#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

1

Pick the max value k

k = 5 sets the outer layer and determines width/height (2*k-1).

Setup
2

Top half: i from k down to 1

This gradually decreases the minimum value allowed in the center until it reaches 1.

Top
3

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.

Symmetry
4

Bottom half: i from 2 up to k

This mirrors the top half around the center row to complete the full pattern.

Bottom
=

Full concentric layers

The grid size is (2k-1)×(2k-1), so runtime grows like O(k²).

2

Variation — User Input Version

Let the user choose k and print the full (2k-1) by (2k-1) pattern.

C++
#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 endl inside loops (extra flushing)
  • Printing invalid sizes (0 or negative) without handling

Key Takeaways

1

Width and height are both 2k-1.

2

The pattern is symmetric top-to-bottom and left-to-right.

3

Two parts (k..1 and 2..k) complete the full shape.

4

Runtime grows like O(k²).

❓ Frequently Asked Questions

Because the second part mirrors the first part, so the last row repeats the first row.
The center row/column clamp to the smallest i, which becomes 1 at the middle.
Yes. Remove the trailing spaces, but for multi-digit numbers you may want setw() instead.
O(k²) because the output is a \((2k-1)×(2k-1)\) grid.

Explore More C++ Number Patterns!

Patterns like this are a fun way to practice symmetry, loops, and matrix-style thinking.

All Number Patterns →
Did you know?

A common alternative solution computes the value at each cell using the minimum distance to any border. Both approaches produce the same concentric layers.

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