Hollow Square Border of 1s in C++

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a hollow square where only the border is filled with 1 and the inside is blank.

This is a great pattern to practice:

  • Row/column thinking (2D loops)
  • Border detection using a simple condition

⭐ Pattern Output

For n = 5, the pattern looks like this:

Output
1 1 1 1 1
1       1
1       1
1       1
1 1 1 1 1
1

Complete C++ Program

Print 1 on the first/last row or first/last column; otherwise print spaces.

C++
#include <iostream>
using namespace std;

int main() {
    int i, j;

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

    return 0;
}

🧠 How It Works

1

Loop through rows

The outer loop chooses the row index i from 1 to 5.

Rows
2

Loop through columns

The inner loop chooses the column index j from 1 to 5.

Columns
3

Detect border cells

If i is 1 or 5, you’re on the top/bottom border. If j is 1 or 5, you’re on the left/right border.

Border check
4

Print 1 or blank

Border cells print "1 ", interior cells print spaces " ".

Output
=

Hollow 1-square

You visit every cell once, so runtime is O(n²) for an n×n square.

2

Variation — User Input Version

Let the user choose the size n at runtime.

C++
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter the value of n: ";
    cin >> n;

    if (n <= 0) return 0;

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Replace 1 with any character (e.g., * or #)
  • Print a hollow square of 0s by changing the border print
  • Use two digits (like 10) and adjust spacing accordingly
  • Add input validation and re-prompt on invalid n
  • Create a hollow rectangle by using separate rows and cols

Avoid

  • Forgetting the newline after each row
  • Using inconsistent spacing (your border won’t align)
  • Allowing n to be 0 or negative without handling it
  • Hard-coding sizes when you already have a variable

Key Takeaways

1

The border is detected with i==1 || i==n || j==1 || j==n.

2

Interior cells print spaces, creating a hollow look.

3

This technique generalizes to hollow rectangles and other borders.

4

Runtime is O(n²) for an n×n grid.

❓ Frequently Asked Questions

A cell is on the border if it is in the first/last row or first/last column. That is exactly what i==1 || i==n || j==1 || j==n checks.
We print "1 " (a 1 plus a trailing space) so the columns line up nicely.
Yes. Remove the if and always print "1 " inside the inner loop.
O(n²) because there are n rows and n columns and each cell is processed once.

Explore More C++ Number Patterns!

Border checks like this show up everywhere—from hollow shapes to matrix problems.

All Number Patterns →
Did you know?

Hollow patterns are a common introduction to 2D grid logic. The same border idea is used in image processing and matrix traversal problems.

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