C Hollow Square Pattern (Border of 1s)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A hollow square border prints 1 only on the first/last row and first/last column of an n × n grid. Interior cells print spaces so the frame stays hollow.

Remember
Rule: for i from 1 to size
        for j from 1 to size
          if i or j is on the border: print "1 "
          else: print "  "
        print newline

1 1 1 1 1
1       1
1       1
1       1
1 1 1 1 1     ← size = 5

Follows the square-number pyramid in Program 41; next is Program 43.

How to Solve It

Start with fixed size = 5, then generalize with scanf — optionally swap the border character for *.

MethodIdeaBest for
Border checkPrint "1 " on edges; " " insideLearning, interviews, exams
Custom characterSame condition; print "* " on the borderQuick variants and demos

Pseudocode

Pseudocode
for i from 1 to size:
    for j from 1 to size:
        if i == 1 or i == size or j == 1 or j == size:
            print "1 " (no newline)
        else:
            print "  " (no newline)
    print newline

Cheat sheet

GoalPattern
Visit every cellfor (i = 1; i <= size; i++) / for (j = 1; j <= size; j++)
Border testi == 1 || i == size || j == 1 || j == size
Print borderprintf("1 ");
Print interiorprintf(" ");
End the rowprintf("\n");

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("1 ") / printf(" ")Stays on the same lineEach cell in the row
printf("\n")Ends the current lineAfter the inner loop

Print all cells without a newline, then end the row once.

Live Preview

Change the square size and the hollow border updates instantly.

Whole numbers from 2 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result 5×5 · 25 cells
1 1 1 1 1 
1       1 
1       1 
1       1 
1 1 1 1 1 

Worked Walkthrough — Row i = 3, size = 5

Trace each column j on a middle row and decide border vs interior.

jOn border?WhyPrints
1Yesj == 11
2NoInterior
3NoInterior
4NoInterior
5Yesj == size1

Row 3 output: 1 1. Total cells for size n = n² (here 25). Border cells for n >= 2 = 4n - 4.

C Programs

Three complete programs: fixed size, scanf size, and an asterisk border. Use View Output to reveal sample results.

Example 1 — Fixed size = 5

Hard-coded 5×5 grid — print "1 " on the border and " " inside.

C
#include <stdio.h>

int main(void)
{
    int i, j;

    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= 5; j++)
        {
            if (i == 1 || i == 5 || j == 1 || j == 5)
                printf("1 ");
            else
                printf("  ");
        }
        printf("\n");
    }

    return 0;
}

How It Works

1. Visit every cell. Nested loops cover all 25 positions in the 5×5 grid.

2. Border test. If the cell sits on the first/last row or column, print "1 "; otherwise print two spaces.

3. End the line. printf("\n") runs only after the inner loop finishes the row.

When i = 1 every cell is a border; when i = 3 and j = 3 you get spaces.

Example 2 — User Input Size

Read size with scanf and reject values of 1 or less so a hollow frame is possible.

C
#include <stdio.h>

int main(void)
{
    int size;
    int i, j;

    printf("Enter square size: ");
    if (scanf("%d", &size) != 1 || size <= 1)
    {
        printf("Please enter an integer greater than 1.\n");
        return 1;
    }

    for (i = 1; i <= size; i++)
    {
        for (j = 1; j <= size; j++)
        {
            if (i == 1 || i == size || j == 1 || j == size)
                printf("1 ");
            else
                printf("  ");
        }
        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and validate. Reject non-integers and sizes ≤ 1 with a clear message.

2. Same core. Only the hard-coded 5 becomes size in both loops and the border check.

3. Safer input tip. You can also clamp the upper bound for demos:

Safer input
if (scanf("%d", &size) != 1 || size < 2 || size > 9)
{
    printf("Enter a whole number from 2 to 9.\n");
    return 1;
}

Example 3 — Asterisk Border

Keep size = 5 but print * on the border instead of 1.

C
#include <stdio.h>

int main(void)
{
    int size = 5;
    int i, j;

    for (i = 1; i <= size; i++)
    {
        for (j = 1; j <= size; j++)
        {
            if (i == 1 || i == size || j == 1 || j == size)
                printf("* ");
            else
                printf("  ");
        }
        printf("\n");
    }

    return 0;
}

How It Works

1. Same structure. Loops and border condition are unchanged.

2. Swap the character. Only "1 " becomes "* " — keep the trailing space for column alignment.

3. Try other symbols. #, @, or 0 work the same way once the hollow frame is clear.

Edge Cases & Pitfalls

Check these before calling the solution done.

no check

Solid square

Printing 1 in every cell fills the grid. Keep the border condition so the interior stays hollow.

one space

Misaligned columns

Interior printf(" ") is only one character wide. Use printf(" ") to match "1 ".

hard-coded 5

Broken NxN input

If the border check still uses 5 after reading size, larger grids look wrong. Use size everywhere.

\n inside

Broken rows

If printf("\n") sits inside the inner loop, each cell lands on its own line. Call the newline only after the row finishes.

size = 1

No hollow interior

Output is a single 1. Prefer size >= 2 for a visible frame.

scanf

Check the return value

If scanf fails, size may be uninitialized — always test scanf(...) == 1.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(size²)O(1)
Asterisk border (Example 3)O(size²)O(1)

Every cell is visited once → n² prints. For size = 5 that is 25 cells (16 on the border).

Key Takeaways

  • Rule: print "1 " when i or j is on the edge; otherwise " ".
  • Match widths: border and interior each print two characters so columns stay aligned.
  • Break the row: call printf("\n") only after the inner loop.
  • Complexity: O(n²) time from n² cells; O(1) extra space.

One line: for each cell, print "1 " on the border or " " inside, then printf("\n") after each row.

Frequently Asked Questions

A hollow square prints characters only on the border (first/last row and first/last column) and leaves the inside blank with spaces.
It checks if i is 1 or size, or if j is 1 or size. If any condition is true, it prints 1; otherwise it prints spaces.
Use a size variable and loop from 1 to size for both i and j. Update the border check to use size instead of 5 — see Example 2.
Each border cell uses "1 " (digit plus space). Interior cells use " " so columns stay aligned in the console.
Size 2 gives a thin border frame. Size 1 prints a single 1 with no hollow interior.
Program 41 prints a centered pyramid of perfect squares. Program 42 prints a hollow square grid using border conditions.
Replace "1 " with "* " or "# " in the if branch — see Example 3.
O(n²) for an n×n grid because each cell is visited once.

Did you know?

Print 1 when i == 1, i == size, j == 1, or j == size; otherwise print spaces. A size × size grid visits n² cells — total prints = n².

Next: Right-Aligned Number Triangle

Continue with ascending digits padded by leading spaces for a right-aligned look.

Program 43 tutorial →

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