Print Hollow Rectangle or Square Star Pattern

Last Updated : 8 Mar, 2026

Given two integers rows and columns, print a hollow rectangle star pattern of the given dimensions. In this pattern, stars (*) are printed on the boundary of the rectangle, while the inner area contains spaces.

Example:

Input: rows = 6, columns = 20
Output:

420851436
Hollow Rectangle
Try it on GfG Practice
redirect icon

Using Nested Loops – O(m × n) Time and O(1) Space

The pattern can be printed using two nested loops. The outer loop iterates through the rows, and the inner loop iterates through the columns. A star (*) is printed when the current position is on the first row, last row, first column, or last column; otherwise, a space is printed.

C++
//Driver Code Starts
using namespace std;

// Function to print hollow rectangle 
//Driver Code Ends

void print_rectangle(int n, int m) 
{ 
    int i, j; 
    for (i = 1; i <= n; i++) 
    { 
        for (j = 1; j <= m; j++) 
        { 
            if (i == 1 || i == n || 
                j == 1 || j == m)         
                cout << "*";             
            else
                cout << " ";
        } 
        cout << endl;
    } 

} 

//Driver Code Starts
int main() 
{ 
    int rows = 6, columns = 20; 
    print_rectangle(rows, columns); 
    return 0; 
} 
//Driver Code Ends
C
//Driver Code Starts
#include <stdio.h>

// Function to print hollow rectangle
//Driver Code Ends

void print_rectangle(int n, int m)
{
    int i, j;
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= m; j++)
        {
            if (i==1 || i==n || j==1 || j==m)            
                printf("*");            
            else
                printf(" ");            
        }
        printf("
");
    }

}

//Driver Code Starts
int main()
{
    int rows = 6, columns = 20;
    print_rectangle(rows, columns);
    return 0;
}
//Driver Code Ends
Java
//Driver Code Starts
class GFG {

    // Function to print hollow rectangle
//Driver Code Ends

    static void print_rectangle(int n, int m)
    {
        int i, j;
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= m; j++) {
                if (i == 1 || i == n || j == 1 || j == m)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }

//Driver Code Starts
    public static void main(String args[])
    {
        int rows = 6, columns = 20;
        print_rectangle(rows, columns);
    }
}
//Driver Code Ends
Python
#Driver Code Starts
# Function to print hollow rectangle
#Driver Code Ends

def print_rectangle(rows, columns):

    for i in range(1, rows + 1):
        for j in range(1, columns + 1):

            # Print star at boundary positions
            if i == 1 or i == rows or j == 1 or j == columns:
                print("*", end="")
            else:
                print(" ", end="")

        # Move to the next line
        print()


#Driver Code Starts

def main():
    rows = 6
    columns = 20
    print_rectangle(rows, columns)


if __name__ == "__main__":
    main()

#Driver Code Ends
C#
//Driver Code Starts
using System;
public class GFG {

//Driver Code Ends

    // Function to print hollow rectangle
    static void print_rectangle(int n, int m)
    {
        int i, j;
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= m; j++) {
                if (i == 1 || i == n || j == 1 || j == m)
                    Console.Write("*");
                else
                    Console.Write(" ");
            }
            Console.WriteLine();
        }
    }


//Driver Code Starts
    public static void Main()
    {
        int rows = 6, columns = 20;
        print_rectangle(rows, columns);
    }
}
//Driver Code Ends
Javascript
// Function to print hollow rectangle
function printRectangle(n, m) {
    let output = "";

    for (let i = 1; i <= n; i++) {
        for (let j = 1; j <= m; j++) {
            if (i === 1 || i === n || j === 1 || j === m)
                output += "*";
            else
                output += " ";
        }
        output += "
";
    }

    console.log(output);
}


//Driver Code Starts
// Driver code
let rows = 6;
let columns = 20;
printRectangle(rows, columns);
//Driver Code Ends
Comment