0
0
CProgramBeginner · 2 min read

C Program to Print Hollow Square Pattern

Use nested for loops in C to print a hollow square pattern by printing stars * on the borders and spaces inside, like if (i == 0 || i == n-1 || j == 0 || j == n-1) printf("*"); else printf(" ");.
📋

Examples

Inputn = 3
Output*** * * ***
Inputn = 5
Output***** * * * * * * *****
Inputn = 1
Output*
🧠

How to Think About It

To print a hollow square, think of a grid with rows and columns. Print stars * on the first and last rows and columns to form the border. For all other positions inside, print spaces to keep it hollow.
📐

Algorithm

1
Get the size of the square, n.
2
Loop from 0 to n-1 for each row.
3
Inside each row, loop from 0 to n-1 for each column.
4
If the current position is on the border (first or last row or column), print a star.
5
Otherwise, print a space.
6
After each row, print a newline.
💻

Code

c
#include <stdio.h>

int main() {
    int n = 5;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i == 0 || i == n-1 || j == 0 || j == n-1)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}
Output
***** * * * * * * *****
🔍

Dry Run

Let's trace n=5 through the code to see how the hollow square is printed.

1

Start outer loop i=0 (first row)

Print stars for all columns j=0 to 4 because i=0 is the first row.

2

i=1 (second row)

Print star at j=0 (first column), spaces for j=1 to 3, star at j=4 (last column).

3

i=2 (third row)

Same as i=1: star at borders, spaces inside.

4

i=3 (fourth row)

Same as i=1 and i=2.

5

i=4 (last row)

Print stars for all columns because i=4 is the last row.

Row (i)Column (j)Print
00-4*****
10*
11-3
14*
20*
21-3
24*
30*
31-3
34*
40-4*****
💡

Why This Works

Step 1: Print border stars

The condition i == 0 || i == n-1 || j == 0 || j == n-1 checks if the current position is on the border to print stars.

Step 2: Print spaces inside

For positions not on the border, printing spaces creates the hollow effect inside the square.

Step 3: Nested loops for rows and columns

Two loops let us visit every position in the square grid row by row and column by column.

🔄

Alternative Approaches

Using while loops
c
#include <stdio.h>

int main() {
    int n = 5, i = 0, j;
    while (i < n) {
        j = 0;
        while (j < n) {
            if (i == 0 || i == n-1 || j == 0 || j == n-1)
                printf("*");
            else
                printf(" ");
            j++;
        }
        printf("\n");
        i++;
    }
    return 0;
}
Uses while loops instead of for loops; functionally same but less concise.
Using a function to print pattern
c
#include <stdio.h>

void printHollowSquare(int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i == 0 || i == n-1 || j == 0 || j == n-1)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
}

int main() {
    printHollowSquare(5);
    return 0;
}
Encapsulates logic in a function for reusability and cleaner main.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses two nested loops each running n times, so it runs in O(n^2) time.

Space Complexity

Only a few variables are used; no extra space grows with input size, so space is O(1).

Which Approach is Fastest?

All approaches use nested loops and have the same time and space complexity; differences are mainly in code style.

ApproachTimeSpaceBest For
For loopsO(n^2)O(1)Simple and clear code
While loopsO(n^2)O(1)Alternative loop style
Function encapsulationO(n^2)O(1)Reusable and organized code
💡
Use nested loops and print stars only on the edges to create a hollow square.
⚠️
Printing stars for all positions without checking borders, resulting in a solid square instead of hollow.