Table of contents
1.
Introduction
2.
Problem Statement
3.
Solution
4.
Solid Rectangular Asterisk Pattern
4.1.
Programming language: C
4.2.
Programming language: C++
4.3.
Programming language: Java
4.4.
Programming language: Python
5.
Hollow Rectangular Asterisk Pattern
5.1.
Programming language: C
5.2.
Programming language: C++
5.3.
Programming language: Java
5.4.
Programming language: Python
6.
Frequently Asked Questions
6.1.
What is the Average Time Complexity of printing solid rectangles using an asterisk?
6.2.
What is the Average Time Complexity of printing Hollow rectangles using an asterisk?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Rectangles using Asterisk

Author Nagendra
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Interview Puzzles

Introduction

The best way to learn how looping structures in general-purpose programming languages function is to use pattern programs. It assists newcomers in visualizing how each loop iteration works. Pattern programs are simply patterns made up of integers, alphabets, or symbols in a specific order. These kinds of pattern programs are simple to solve when employing the for loop condition. These are often asked questions in campus placements and career interviews.

Problem Statement

Write a program to create hollow rectangular asterisk patterns. For example for a rectangle of length = 4 units and breadth = 2 units the output should be:

****
****

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 :

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 :
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 :

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 :

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!

Live masterclass