Program to print a rectangle pattern Last Updated : 24 Feb, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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<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. 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 ?> JavaScript // Javascript program to print a rectangular pattern function printRectangle(h, w) { for(var i=0; i<h; i++) { console.log("\n"); for(var 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.log("@"); else console.log(" "); } } } // Driver code var h = 4, w = 5; printRectangle(h, w); <script> // 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. Comment More infoAdvertise with us Next Article Program to print pattern A Anurag Rawat Improve Article Tags : Misc DSA Basic Coding Problems pattern-printing square-rectangle +1 More Practice Tags : Miscpattern-printing Similar Reads Program to print the arrow pattern Given the value of n, print the arrow pattern.Examples : Input : n = 5 Output : * ** *** **** ***** **** *** ** * Input : n = 7 Output : * ** *** **** ***** ****** ******* ****** ***** **** *** ** * Below is the program to print the arrow pattern: C++ // C++ program to print the // arrow pattern #in 10 min read Program to print the pattern "GFG" In this article, given the value of n(length of the alphabet) and k(width of the alphabet) we will learn how to print the pattern "GFG" using stars and white-spaces. Examples: INPUT: n=7, k=5 OUTPUT: ***** ***** ***** * * * * * * * ** ***** * *** * * * * * * * * * * ***** * ***** INPUT: n=11, k=7 OU 8 min read Program to print pattern Given the value of n, print the following pattern.Examples : Input : n = 4 Output : A1 AB12 ABC123 ABCD1234 Input : n = 7 Output : A1 AB12 ABC123 ABCD1234 ABCDE12345 ABCDEF123456 ABCDEFG1234567 Below is the implementation to print the above pattern : C++ // C++ program to print given pattern #includ 5 min read C++ Program To Print Pyramid Patterns In this article, we will discuss the following top 16 pattern programs in C++ using star ( * ), numbers or other characters. Table of ContentSimple Pyramid Pattern in C++Flipped Simple Pyramid Pattern in C++Inverted Pyramid Pattern in C++Flipped Inverted Pyramid Pattern in C++Triangle Pattern in C++ 15+ min read Program to print pyramid pattern Write to program to print the pyramid pattern formed of stars Example : Input: n = 6 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * We strongly recommend you to minimize your browser and try this yourself first.The idea is to use two for loops for every part of the p 5 min read Program to print Sine-Wave Pattern Given two integers waveHeight and waveLength. The task is to print a sine wave pattern using the character 0. The sine wave rises and falls vertically across the given height and repeats horizontally for the given length. Examples:Input: waveHeight = 5, waveLength = 10Output: 0 0 0 0 0 0 0 0 0 0 0 0 15+ min read Like