Solution
There can be two or more loops in each pattern program. The number of loops required is determined by the pattern's complexity. The first loop affects the row, whereas the second affects the column.
Solid Rectangular Asterisk Pattern
The following code illustrates printing a Solid rectangular Asterisk Pattern:
Programming language: C
The following code illustrates printing solid rectangle using asterisk in C :
#include<stdio.h>
int main()
{
int length=4;
int width = 3;
for (int a = 0; a < length; a++) //This loop is for number of rows
{
for (int b = 0; b < width; b++) //loop for number of columns
{
printf("*"); //printing asterisk
}
printf("\n"); //ending line
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output :
***
***
***
***
Programming language: C++
The following code illustrates printing a solid rectangle using an asterisk in C++ :
#include <iostream>
using namespace std;
int main()
{
int length=4;
int width = 3;
for (int a = 0; a < length; a++) //loop for number of rows
{
for (int b = 0; b < width; b++) //loop for number of columns
{
cout<<"*"; //printing asterisk
}
cout<<"\n"; //ending line
}
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output :
***
***
***
***
Programming language: Java
The following code illustrates printing a solid rectangle using an asterisk in Java :
public class Ninja {
public static void main(String[] args) {
int length=4;
int width = 3;
for (int a = 0; a < length; a++) //loop for number of rows
{
for (int b = 0; b < width; b++) //loop for number of columns
{
System.out.print("*"); //printing asterisk
}
System.out.println(); //ending line
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output :
***
***
***
***
Programming language: Python
The following code illustrates printing a solid rectangle using an asterisk in Python :
length=4;
width = 3;
for i in range(length): #loop for number of rows
for j in range(width):#loop for number of columns
print("*",end=''); #printing asterisk
print(); #ending line

You can also try this code with Online Python Compiler
Run Code
Output :
***
***
***
***
Hollow Rectangular Asterisk Pattern
The following code illustrates printing a Hollow Rectangular Asterisk Pattern:
Programming language: C
The following code illustrates printing a hollow rectangle using an asterisk in C :
#include <stdio.h>
int main() {
int length=5,width=7;
for (int a = 0; a < length; a++) //loop for number of rows
{
for (int b = 0; b < width; b++) //loop for number of columns
{
if (a==0 || a==length-1 || b==0 || b==width-1)
printf("*"); //printing asterisk
else
printf(" "); //printing spaces for hollow area
}
printf("\n"); //ending line
}
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output :

Programming language: C++
The following code illustrates printing a hollow rectangle using an asterisk in C++ :
#include <iostream>
int main() {
int length=5,width=7;
for (int a = 0; a < length; a++) //loop for number of rows
{
for (int b = 0; b < width; b++) //loop for number of columns
{
if (a==0 || a==length-1 || b==0 || b==width-1)
cout<<"*"; //printing asterisk
else
cout<<" "; //printing spaces for hollow area
}
cout<<endl; //ending line
}
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output :

Programming language: Java
The following code illustrates printing a hollow rectangle using an asterisk in Java :
public class Ninja {
public static void main(String[] args) {
int length=5;
int width = 7;
for (int a = 0; a < length; a++) //loop for number of rows
{
for (int b = 0; b < width; b++) //loop for number of columns
{
if (a==0 || a==length-1 || b==0 || b==width-1)
System.out.print("*"); //printing asterisk
else
System.out.print(" "); //printing spaces for hollow area
}
System.out.println(); //ending line
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output :

Programming language: Python
The following code illustrates printing hollow rectangle using asterisk in Python :
length=5;
width = 7;
for i in range(length): #loop for number of rows
for j in range(width):#loop for number of columns
if (i==0 or i==length-1 or j==0 or j==width-1):
print("*", end=''); #printing asterisk
else :
print(" ",end=''); #printing spaces for hollow area
print(); #ending line

You can also try this code with Online Python Compiler
Run Code
Output :

You can practice by yourself with the help of online python compiler for better understanding.
Frequently Asked Questions
What is the Average Time Complexity of printing solid rectangles using an asterisk?
The Time Complexity of printing solid rectangles using an asterisk is O(n2).
What is the Average Time Complexity of printing Hollow rectangles using an asterisk?
The Time Complexity of printing hollow rectangles using an asterisk is O(n2).
Conclusion
In this article, we have extensively discussed printing rectangles using asterisk and their implementation in different programming languages. The article explains the different approaches used to print rectangles using asterisk viz. printing solid rectangular asterisk and printing hollow rectangular asterisk. There are many more approaches, but these are the most efficient in time and space.
Recommended Readings:
Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Coding Ninjas Studio.
Also check out some of the Guided Paths on topics such as Interview Puzzles, Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.
Do upvote our blog to help other ninjas grow.
Happy Coding!