Program to print a rectangle pattern
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++
// 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 code int main() { int h = 4, w = 5; printRectangle(h, w); return 0; } |
chevron_right
filter_none
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.*/ |
chevron_right
filter_none
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 = 4w = 5printRectangle(h, w) # This code is contributed by Nikita Tiwari. |
chevron_right
filter_none
C#
// C# program to print a rectangular // pattern using 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.*/ |
chevron_right
filter_none
PHP
<?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 ?> |
chevron_right
filter_none
Output:
@@@@@ @ @ @ @ @@@@@
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.
Recommended Posts:
- Program to print hollow rectangle or square star patterns
- Program to print the pattern 'G'
- Program to print the given H Pattern
- Program to print the pattern "GFG"
- Program to print pattern
- Program to print the pattern ‘D’
- Program to print the given Z Pattern
- Program to print Hut Star pattern
- Program to print numeric pattern
- Program to print the arrow pattern
- Program to print circle pattern
- Program to print Step Pattern
- Program to print a pattern of numbers
- Program to print Kite Pattern
- Program to print pyramid pattern
Improved By : Mithun Kumar



