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.
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 Startsusingnamespacestd;// Function to print hollow rectangle //Driver Code Endsvoidprint_rectangle(intn,intm){inti,j;for(i=1;i<=n;i++){for(j=1;j<=m;j++){if(i==1||i==n||j==1||j==m)cout<<"*";elsecout<<" ";}cout<<endl;}}//Driver Code Startsintmain(){introws=6,columns=20;print_rectangle(rows,columns);return0;}//Driver Code Ends
C
//Driver Code Starts#include<stdio.h>// Function to print hollow rectangle//Driver Code Endsvoidprint_rectangle(intn,intm){inti,j;for(i=1;i<=n;i++){for(j=1;j<=m;j++){if(i==1||i==n||j==1||j==m)printf("*");elseprintf(" ");}printf("");}}//Driver Code Startsintmain(){introws=6,columns=20;print_rectangle(rows,columns);return0;}//Driver Code Ends
Java
//Driver Code StartsclassGFG{// Function to print hollow rectangle//Driver Code Endsstaticvoidprint_rectangle(intn,intm){inti,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("*");elseSystem.out.print(" ");}System.out.println();}}//Driver Code Startspublicstaticvoidmain(Stringargs[]){introws=6,columns=20;print_rectangle(rows,columns);}}//Driver Code Ends
Python
#Driver Code Starts# Function to print hollow rectangle#Driver Code Endsdefprint_rectangle(rows,columns):foriinrange(1,rows+1):forjinrange(1,columns+1):# Print star at boundary positionsifi==1ori==rowsorj==1orj==columns:print("*",end="")else:print(" ",end="")# Move to the next lineprint()#Driver Code Startsdefmain():rows=6columns=20print_rectangle(rows,columns)if__name__=="__main__":main()#Driver Code Ends
C#
//Driver Code StartsusingSystem;publicclassGFG{//Driver Code Ends// Function to print hollow rectanglestaticvoidprint_rectangle(intn,intm){inti,j;for(i=1;i<=n;i++){for(j=1;j<=m;j++){if(i==1||i==n||j==1||j==m)Console.Write("*");elseConsole.Write(" ");}Console.WriteLine();}}//Driver Code StartspublicstaticvoidMain(){introws=6,columns=20;print_rectangle(rows,columns);}}//Driver Code Ends
Javascript
// Function to print hollow rectanglefunctionprintRectangle(n,m){letoutput="";for(leti=1;i<=n;i++){for(letj=1;j<=m;j++){if(i===1||i===n||j===1||j===m)output+="*";elseoutput+=" ";}output+="
";}console.log(output);}//Driver Code Starts// Driver codeletrows=6;letcolumns=20;printRectangle(rows,columns);//Driver Code Ends