The number frame layers pattern prints nested square frames numbered from the outside in: 1 on the border, 2 on the next ring and 3 in the centre. It is the concentric square number pattern with the numbering turned around.
Quick answer: For every cell, find its distance to the nearest edge: the smallest of i β 1, j β 1, w β i and w β j, where w = 2n β 1. Print that distance plus one. The border is distance 0 and prints 1; the centre prints n.
This program is part of the Spiral Patterns collection.
Table of contents8 sections
Number Frame Layers Pattern output
Output (n = 3 (5 Γ 5)):
1 1 1 1 1 1 2 2 2 1 1 2 3 2 1 1 2 2 2 1 1 1 1 1 1

Same shape, other characters: the Number and Star Checker Grid, Square Star Pattern and String Plus Pattern programs draw this same outline with different characters. Putting them side by side shows which part of the loop decides the shape and which part decides what is printed.
How the number frame layers pattern works
All cells on the same square ring have the same distance to the nearest edge, so one small calculation labels the whole ring. The concentric number pattern prints n minus this distance, which puts the largest number on the outside. Printing the distance plus one reverses that, so the numbers grow towards the middle.
- Set the width
w = 2n β 1. - For each cell, set
dto the smallest ofi β 1,j β 1,w β iandw β j. - Print
d + 1followed by a space. - Move to the next line after each row.
Key takeaways
- Cells on one ring share the same distance to the edge.
- Printing
d + 1numbers the rings from the outside in. - Printing
n β dinstead gives the concentric square number pattern. - The grid is always odd-sized,
2n β 1, so there is one centre cell.
Number Frame Layers Pattern in C
#include <stdio.h>
int main(void) {
int n = 3;
int w = 2 * n - 1;
for (int i = 1; i <= w; i++) {
for (int j = 1; j <= w; j++) {
int d = i - 1; /* distance to the nearest edge */
if (j - 1 < d) d = j - 1;
if (w - i < d) d = w - i;
if (w - j < d) d = w - j;
printf("%d ", d + 1);
}
printf("\n");
}
return 0;
}Number Frame Layers Pattern in C++
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n = 3;
int w = 2 * n - 1;
for (int i = 1; i <= w; i++) {
for (int j = 1; j <= w; j++) {
int d = min(min(i - 1, j - 1), min(w - i, w - j)); // distance to the nearest edge
cout << d + 1 << " ";
}
cout << endl;
}
return 0;
}Number Frame Layers Pattern in Java
public class NumberFrameLayers {
public static void main(String[] args) {
int n = 3;
int w = 2 * n - 1;
for (int i = 1; i <= w; i++) {
for (int j = 1; j <= w; j++) {
int d = Math.min(Math.min(i - 1, j - 1), Math.min(w - i, w - j)); // distance to the nearest edge
System.out.print((d + 1) + " ");
}
System.out.println();
}
}
}Number Frame Layers Pattern in Python
n = 3
w = 2 * n - 1
for i in range(1, w + 1):
for j in range(1, w + 1):
d = min(i - 1, j - 1, w - i, w - j) # distance to the nearest edge
print(d + 1, end=" ")
print()Time and space complexity
Each of the (2n β 1)Β² cells needs a constant amount of work, so the program runs in O(nΒ²) time with O(1) extra memory.
Frequently asked questions
How is it different from the concentric square number pattern?
Same rings, opposite numbering. The concentric version puts n on the border and 1 in the centre.
How do I print the rings as letters?
Print 'A' + d. The border becomes A, the next ring B and the centre C.
Why is the C version written with if statements?
C has no built-in min for integers, so the program takes the smallest of the four distances with three comparisons. C++, Java and Python use their library min functions.






