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

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 − 1by2n − 1: 7 × 7 forn = 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.








