C++ Program to Print Hollow Square Pattern



When we start learning to code, practicing star patterns is one of the best ways to improve logic building. One of the simplest yet fascinating patterns is the hollow square pattern. In this article, we are going to learn how to print a Hollow Square Pattern using C++.

What is Hollow Square Pattern?

A hollow square pattern is a geometrical arrangement of stars forming a square shape, but the stars are present only at the boundary of the square. The stars are printed in such a manner that the edges of the square are filled with stars, while the interior is left empty.

The hollow square star pattern looks like the below figure:

*****
*   *
*   *
*   *
*****

Below are different approaches to printing hollow square star pattern in C++

Print Hollow Square Pattern Using Boundary Conditions Approach

In this approach, we first input the number of rows and columns to print the hollow square. We use a nested loop where the outer loop handles the rows of the pattern and the inner loop handles the columns of each row. The first and last rows are filled with stars and in the middle rows, only the first and last columns are filled with stars, while the rest are empty spaces.

Steps for Implementation

  1. We first take the input number of rows and columns.
  2. Now use nested loops to print the hollow square pattern where the outer loop iterates through the rows and the inner loop checks whether the current cell lies on the boundary.
  3. Print stars for boundary cells and spaces for the rest interior cells.
  4. Move to the next line after processing each row.

Implementation Code

#include<bits/stdc++.h>
using namespace std;

int main() {
  int rows = 5;
  int cols = 5;

  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
      // Print '*' for boundary cells 
      if (i == 0 || i == rows - 1 || j == 0 || j == cols - 1) {
        cout << "*";
      } else {
        // Print space for interior cells 
        cout << " ";
      }
    }
    cout << endl;
  }

  return 0;
}

Output:

*****
*     *
*     *
*     *
*****

Time Complexity: O(n × m):
Space Complexity: O(1)

Print Hollow Square Pattern Using String Concatenation Approach

In this approach, we use a string concatenation approach to print a hollow parallelogram star pattern. The logic used in this approach is easier to understand and makes the code more concise.

Steps for Implementation

  1. First, input the number of rows and columns.
  2. Now, use the outer loop to iterate through each row.
  3. For the first and last rows, create a string of stars.
  4. Create a string with a star, spaces, and a star at the end for all other rows.
  5. Print the strings row by row.

Implementation Code

#include<bits/stdc++.h>
using namespace std;

int main() {
  int rows = 5;
  int cols = 5;

  for (int i = 0; i < rows; i++) {
    if (i == 0 || i == rows - 1) {
      // Create a string of stars for the first and last rows
      cout << string(cols, '*') << endl;
    } else {
      // Create a string for the middle rows
      cout << "*" + string(cols - 2, ' ') + "*" << endl;
    }
  }
  return 0;
}

Output

*****
*     *
*     *
*     *
*****

Time Complexity: O(n * m)
Space Complexity: O(n + m)

Why should We Practice these star printing patterns?

Printing star patterns such as the above hollow square pattern helps us in developing a clear understanding of nested loops and conditional logic. We should practice different shape-type patterns to build clearer logical and thinking skills.

Updated on: 2025-01-23T23:11:31+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements