Program to print a rectangle pattern - GeeksforGeeks

04 Oct, 2018
Given height h and width w, print a rectangular pattern as shown in example below.
Examples:
Input : h = 4, w = 5
Output : @@@@@
         @   @
         @   @
         @@@@@

Input : h = 7, w = 9
Output : @@@@@@@@
         @      @
         @      @
         @      @
         @      @
         @      @
         @@@@@@@@

The idea is to run two loops. One for number of rows to be printed and other for number of columns. Print a ‘@’ only when current row is first or last. OR current column is first or last.
C++
Java
Python3
C#
PHP
// CPP program to print a rectangular pattern#include<iostream>using namespace std;  void printRectangle(int h, int w){    for (int i=0; i<h; i++)    {        cout << "\n";        for (int j=0; j<w; j++)        {            // Print @ if this is first row            // or last row. Or this column            // is first or last.            if (i == 0 || i == h-1 ||                 j== 0 || j == w-1)               cout << "@";            else               cout << " ";        }    }}  // Driver codeint main(){      int h = 4, w = 5;    printRectangle(h, w);     return 0;}
// JAVA program to print a rectangular // pattern  class GFG {          static void printRectangle(int h, int w)    {        for (int i = 0; i < h; i++)        {            System.out.println();            for (int j = 0; j < w; j++)            {                // Print @ if this is first                 // row or last row. Or this                // column is first or last.                if (i == 0 || i == h-1 ||                    j== 0 || j == w-1)                   System.out.print("@");                else                   System.out.print(" ");            }        }    }           // Driver code    public static void main(String args[])    {          int h = 4, w = 5;        printRectangle(h, w) ;    }}  /*This code is contributed by Nikita Tiwari.*/
# Python 3 program to print a rectangular# pattern  def printRectangle(h, w) :    for i in range(0, h) :        print ("")        for j in range(0, w) :            # Print @ if this is first row            # or last row. Or this column            # is first or last.            if (i == 0 or i == h-1 or j== 0 or j == w-1) :                print("@",end="")            else :                print(" ",end="")             # Driver codeh = 4w = 5printRectangle(h, w)   # This code is contributed by Nikita Tiwari.
// C# program to print a rectangular // patternusing System;class GFG {          static void printRectangle(int h, int w)    {        for (int i = 0; i < h; i++)        {            Console.WriteLine();            for (int j = 0; j < w; j++)            {                // Print @ if this is first                 // row or last row. Or this                // column is first or last.                if (i == 0 || i == h-1 ||                    j== 0 || j == w-1)                Console.Write("@");                else                Console.Write(" ");            }        }    }          // Driver code    public static void Main()    {         int h = 4, w = 5;        printRectangle(h, w) ;    }}  /*This code is contributed by vt_m.*/
<?php// php program to print // a rectangular pattern  function printRectangle($h , $w){    for ($i = 0; $i < $h; $i++)    {        echo"\n";        for ($j = 0; $j < $w; $j++)        {                          // Print @ if this is first row            // or last row. Or this column            // is first or last.            if ($i == 0 || $i == $h - 1 ||                 $j == 0 || $j == $w - 1)            echo"@";            else            echo" ";        }    }}      // Driver code    $h = 4;    $w = 5;    printRectangle($h, $w);   // This code is contributed by mits ?>
   
@@@@@
@   @
@   @
@@@@@

The time complexity is O(n2).This article is contributed by Anurag Rawat. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Mithun Kumar
pattern-printing
square-rectangle
School Programming
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C++ Classes and Objects
Interfaces in Java
Constructors in C++
Operator Overloading in C++
Copy Constructor in C++
Polymorphism in C++
C++ Data Types
Friend class and function in C++
Introduction To PYTHON
Types of Operating Systems