Concentric Number Pattern in C++

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

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:

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
1

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

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

1

Set the max value

k = 5 decides the outer value and also the number of rows.

Setup
2

Row loop chooses i

The outer loop runs i from k down to 1. Smaller i means the center gets smaller.

Rows
3

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.

Left side
4

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.

Right side
=

Concentric look

The minimum value moves toward the center as the rows progress, creating a layered effect.

2

Variation — User Input Version

Let the user choose k. The width becomes 2*k-1.

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";
    }

    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 endl inside loops (extra flushing)
  • Printing negative or zero sizes without handling

Key Takeaways

1

Each row is a mirror of descending then ascending values.

2

The condition j > i decides whether to print j or clamp to i.

3

Width is 2*k-1 for this half-square version.

4

Runtime is O(k²) for size k.

❓ Frequently Asked Questions

Because when i = 1, the clamp value is 1, so the descending half reaches 1 at the center and then mirrors back upward.
Yes. Remove the " " after each number, but alignment becomes harder to read for multi-digit values.
Print 2*k-1 rows and compute each cell based on the minimum distance to the border (classic concentric square approach).
O(k²) since we print roughly k × (2*k-1) values.

Explore More C++ Number Patterns!

Mirror logic like this is useful for many symmetric patterns and matrix-style problems.

All Number Patterns →
Did you know?

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.

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