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; } |
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. |
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.*/ |
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 ?> |
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.