GeeksforGeeks App
Open App
Browser
Continue

# Program to print a rectangle pattern

Given height h and width w, print a rectangular pattern as shown in the example below.

Examples:

```Input  : h = 4, w = 5
Output : @@@@@
@   @
@   @
@@@@@

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

The idea is to run two loops. One for the number of rows to be printed and the other for the number of columns. Print a ‘@’ only when the current row is first or last. OR the current column is first or last.

## C++

 `// CPP program to print a rectangular pattern``#include``using` `namespace` `std;` `void` `printRectangle(``int` `h, ``int` `w)``{``    ``for` `(``int` `i=0; i

## Java

 `// 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.*/`

## Python3

 `# 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 code``h ``=` `4``w ``=` `5``printRectangle(h, w)` `# This code is contributed by Nikita Tiwari.`

## PHP

 ``

## Javascript

 `// Javascript program to print a rectangular pattern` `function` `printRectangle(h, w)``{``    ``for``(``var` `i=0; i` `// This code is contributed by Abhijeet Kumar(abhijeet19403)`

## C#

 `using` `System;` `public` `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(``string``[] args)``    ``{``        ``int` `h = 4, w = 5;``        ``PrintRectangle(h, w);``    ``}``}`

Output

```@@@@@
@   @
@   @
@@@@@```

Time complexity: O(n2), Space Complexity: O(1) //  Constant space.

This article is contributed by Anurag Rawat. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up