Saturday, November 16, 2024
Google search engine
HomeData Modelling & AIProgram to print hollow rectangle or square star patterns

Program to print hollow rectangle or square star patterns

Hollow rectangle star pattern :

The task is print below hollow pattern of given dimension. 
 

********************
*                  *
*                  *
*                  *
*                  *
********************

Explanation: 

  • Input number of rows and columns.
  • For rows of rectangle run the outer loop from 1 to rows.
for (i = 1; i < = rows; i++)
  • For column of rectangle run the inner loop from 1 to columns.  
for (j = 1; j < = columns; j++)
  • Print star for first or last row or for first or last column, otherwise print blank space.
  • After printing all columns of a row, print new line after inner loop.

Below is the implementation:

C++




// C++ code for hollow rectangle
#include <bits/stdc++.h>
using namespace std;
 
// Function to print hollow rectangle
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
int main()
{
    int rows = 6, columns = 20;
    print_rectangle(rows, columns);
    return 0;
}
 
// This code is contributed
// by rathbhupendra


C




// C code for hollow rectangle
#include <stdio.h>
 
// Function to print hollow rectangle
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("\n");
    }
 
}
 
// Driver program for above function
int main()
{
    int rows = 6, columns = 20;
    print_rectangle(rows, columns);
    return 0;
}


Java




// JAVA code for hollow rectangle
import java.io.*;
 
class GFG {
     
    // 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)           
                    System.out.print("*");           
                else
                    System.out.print(" ");           
            }
            System.out.println();
        }
      
    }
      
    // Driver program for above function
    public static void main(String args[])
    {
        int rows = 6, columns = 20;
        print_rectangle(rows, columns);
    }
}
 
/*This code is contributed by Nikita Tiwari.*/


Python3




# Python 3 code for hollow rectangle
 
# Function to print hollow rectangle
def print_rectangle(n, m) :
     
    for i in range(1, n+1) :
        for j in range(1, m+1) :
            if (i == 1 or i == n or
                j == 1 or j == m) :
                print("*", end="")           
            else :
                print(" ", end="")           
         
        print()
 
 
# Driver program for above function
rows = 6
columns = 20
print_rectangle(rows, columns)
 
 
# This code is contributed by Nikita Tiwari.


C#




using System;
public class GFG
{
 
  // 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 program for above function
  public static void Main()
  {
    int rows = 6, columns = 20;
    print_rectangle(rows, columns);
  }
}
 
// This code is contributed by ksrikanth0498.


PHP




<?php
// PHP code for hollow rectangle
 
// Function to print hollow rectangle
function print_rectangle($n, $m)
{
    $i;
    $j;
    for ($i = 1; $i <= $n; $i++)
    {
        for ($j = 1; $j <= $m; $j++)
        {
            if ($i == 1 || $i == $n ||
                $j == 1 || $j == $m)        
                echo("*");        
            else
                echo(" ");        
        }
        echo("\n");
    }
 
}
 
    // Driver Code
    $rows = 6;
    $columns = 20;
    print_rectangle($rows, $columns);
 
// This code is contributed by nitin mittal
?>


Javascript




<script>
      // JavaScript code for hollow rectangle
      // Function to print hollow rectangle
      function print_rectangle(n, m)
      {
        var i, j;
        for (i = 1; i <= n; i++)
        {
          for (j = 1; j <= m; j++)
          {
            if (i == 1 || i == n || j == 1 || j == m)
                 document.write("*");
            else
                document.write("  ");
          }
          document.write("<br>");
        }
      }
 
      // Driver Code
      var rows = 6,
        columns = 20;
      print_rectangle(rows, columns);
       
      // This code is contributed by rdtank.
    </script>


Output

********************
*                  *
*                  *
*                  *
*                  *
********************

Time Complexity: O(m * n), where m and n represents the given inputs.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments