New: 77 pattern programs with tested code in C, C++, Java and Python Explore patterns

Concentric Square Number Pattern

Read summary with AI tools

Square rings of numbers: n on the outer ring, counting down to 1 in the centre. Below you will see how the pattern is built, followed by complete programs in C, C++, Java and Python.

Quick answer: Make a grid 2n − 1 cells wide. For each cell, find its distance to the nearest edge, d = the smallest of i, j, size − 1 − i and size − 1 − j, and print n − d. The outer ring shows n, each ring further in is one smaller, and the centre is 1.

This program is part of the Spiral Patterns collection.

Table of contents8 sections
  1. Output
  2. How the concentric square number pattern works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Concentric Square Number Pattern output

Output (n = 4):

4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4
Concentric Square Number Pattern layout diagram, row by row (n = 4)
How each row of the concentric square number pattern is built (n = 4). Blue cells are printed characters; grey dots are spaces.

How the concentric square number pattern works

The grid is 2n − 1 wide. For each cell, find its distance d to the nearest edge, the smallest of i, j, size − 1 − i and size − 1 − j, and print n − d.

Key takeaways

  • The distance to the nearest edge tells which ring a cell is in.
  • All cells in one ring share the same distance, so they print the same number.
  • The grid is 2n − 1 by 2n − 1: 7 × 7 for n = 4.
  • The loops start at 0 here, so the edges are at index 0 and size − 1.

Concentric Square Number Pattern in C

#include <stdio.h>

int main(void) {
    int n = 4;
    int size = 2 * n - 1;

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            /* distance to the nearest edge */
            int d = i;
            if (j < d) d = j;
            if (size - 1 - i < d) d = size - 1 - i;
            if (size - 1 - j < d) d = size - 1 - j;
            printf("%d ", n - d);
        }
        printf("\n");
    }
    return 0;
}

Concentric Square Number Pattern in C++

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int n = 4;
    int size = 2 * n - 1;

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            // distance to the nearest edge
            int d = min(min(i, j), min(size - 1 - i, size - 1 - j));
            cout << n - d << " ";
        }
        cout << endl;
    }
    return 0;
}

Concentric Square Number Pattern in Java

public class ConcentricSquareNumbers {
    public static void main(String[] args) {
        int n = 4;
        int size = 2 * n - 1;

        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                // distance to the nearest edge
                int d = Math.min(Math.min(i, j), Math.min(size - 1 - i, size - 1 - j));
                System.out.print((n - d) + " ");
            }
            System.out.println();
        }
    }
}

Concentric Square Number Pattern in Python

n = 4
size = 2 * n - 1

for i in range(size):
    for j in range(size):
        # distance to the nearest edge
        d = min(i, j, size - 1 - i, size - 1 - j)
        print(n - d, end=" ")
    print()

Time and space complexity

The program prints one character for each position in the pattern, so for n rows it runs in O(n²) time. It only needs a few loop variables, so the extra memory is O(1).

Frequently asked questions

Why take the smallest of four distances?

A cell is only as deep as its closest edge. The minimum of the distances to the top, left, bottom and right edges gives its ring.

How do I put 1 on the outside instead?

Print d + 1 instead of n − d: the outer ring becomes 1 and the centre becomes n.

What changes for n above 9?

The rings with two-digit numbers become wider; print each number with a fixed width such as %3d.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Latest articles

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.