Hollow Rectangle in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
int main(){
    int r=4,c=6;
    for(int i=0;i<r;i++,puts(""))
        for(int j=0;j<c;j++)
            printf((i==0||i==r-1||j==0||j==c-1)?"* ":"  ");
}

C Output

Input:  
Rows = 4  
Cols = 6  

Output:  
* * * * * *  
*           *  
*           *  
* * * * * *


C++ Program

#include <iostream>
using namespace std;
int main(){
    int r=3,c=5;
    for(int i=0;i<r;i++,cout<<"\n")
        for(int j=0;j<c;j++)
            cout<<((i==0||i==r-1||j==0||j==c-1)?"* ":"  ");
}

C++ Output

Input:  
Rows = 3  
Cols = 5 

Output:  
* * * * *  
*       *  
* * * * *


JAVA Program

public class Main{
    public static void main(String[] a){
        int r=5,c=7;
        for(int i=0;i<r;i++,System.out.println())
            for(int j=0;j<c;j++)
                System.out.print((i==0||i==r-1||j==0||j==c-1)?"* ":"  ");
    }
}

JAVA Output

Input:  
Rows = 5  
Cols = 7  

Output:  
* * * * * * *  
*             *  
*             *  
*             *  
* * * * * * *


Python Program

r,c = 6,4
for i in range(r):
    print("* " * c if i in (0,r-1) else "* " + "  "*(c-2) + "* ")

Python Output

Input:  
Rows = 6  
Cols = 4  

Output:  
* * * *  
*     *  
*     *  
*     *  
*     *  
* * * *


In-Depth Explanation
Example
If Rows = 4, Cols = 6, then the first and last row are filled with *. The middle rows have * only in the first column and the last column, with spaces in between. This gives the "hollow" effect.

Real-Life Analogy
Imagine constructing a picture frame. The wooden frame serves as the border (stars), and the interior is empty space for the picture to be placed in. Just the same, the hollow rectangle's stars serve as the border, and the interior is merely spaces.

Why It Matters
This problem introduces conditional logic within nested loops. You're not printing blindly anymore — you're choosing whether to print * or space depending on the position (i, j). This introduces the fundamental idea of boundary detection.

Learning Insights
The condition (i==0 || i==rows-1 || j==0 || j==cols-1) verifies whether we are in the first row, last row, first column, or last column. If true, print a star; else, print a space. This basic condition is a pattern printing superpower — it can be modified to print boxes, grids, and UI borders for terminal apps.

Real-World Application
This is applied in table borders in CLI applications, game limits, and even generating printable forms when you do not want to fill but only outline shapes.

Interview & Competitive Programming Context
This is an upgrade from the solid rectangle and checks if the candidate is able to add conditions on loop output. Interviewers utilize it in order to test your loop + if condition merging skills, which come in handy later with matrices, image processing, and custom-shaped data rendering.

A hollow rectangle pattern program is an essential beginner-level coding exercise that enhances nested loop expertise and adds conditional logic. It's commonly implemented in coding interviews, competitive programming, and practical use such as CLI layout and table styling. Regardless of whether it is coded in C, C++, Java, or Python, mastery of this pattern enables beginners to move away from basic prints towards intricate graphics and matrix-related problems.