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

Number Frame Layers Pattern (1 1 1, 1 2 1)

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
  1. Number Frame Layers Pattern output
  2. How the number frame layers pattern works
  3. Number Frame Layers Pattern in C
  4. Number Frame Layers Pattern in C++
  5. Number Frame Layers Pattern in Java
  6. Number Frame Layers Pattern in Python
  7. Time and space complexity
  8. Frequently asked questions

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
Number Frame Layers Pattern layout diagram, row by row (n = 3 (5 Γ— 5))
How each row of the number frame layers pattern is built (n = 3 (5 Γ— 5)). Blue cells are printed characters; grey dots are spaces.

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.

  1. Set the width w = 2n βˆ’ 1.
  2. For each cell, set d to the smallest of i βˆ’ 1, j βˆ’ 1, w βˆ’ i and w βˆ’ j.
  3. Print d + 1 followed by a space.
  4. Move to the next line after each row.

Key takeaways

  • Cells on one ring share the same distance to the edge.
  • Printing d + 1 numbers the rings from the outside in.
  • Printing n βˆ’ d instead 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.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 11 of 14 in Spiral Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

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.

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.