Pattern to print X in a rectangular box Last Updated : 20 Feb, 2023 Comments Improve Suggest changes Like Article Like Report Given the value of length, print the X pattern in a box using # and " " Examples: Input : 10 Output : ########## ## ## # # # # # # # # # ## # # ## # # # # # # # # # ## ## ########## Input : 7 Output : ####### ## ## # # # # # # # # # # # ## ## ####### Below is the implementation to print X in a rectangular box pattern: C++ // CPP code to print the above // specified pattern #include <bits/stdc++.h> using namespace std; // Function to print pattern void pattern(int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == 0 || i == n - 1 || j == 0 || j == n - 1 || i == j || i == n - 1 - j) cout << "#"; else cout << " "; } cout << endl; } } // Driver program int main() { int n = 9; pattern(n); return 0; } Java // Java code to print the above // specified pattern import java.io.*; public class GFG { // Function to print pattern static void pattern(int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == 0 || i == n - 1 || j == 0 || j == n - 1 || i == j || i == n - 1 - j) System.out.print("#"); else System.out.print(" "); } System.out.println(); } } // Driver Code public static void main(String args[]) { int n = 9; pattern(n);; } } // This code is contributed by Sam007 Python 3 # Python3 code to print the above # specified pattern # Function to print pattern def pattern(n): for i in range(0, n): for j in range(0, n): if (i == 0 or i == n - 1 or j == 0 or j == n - 1 or i == j or i == n - 1 - j): print( "#", end="") else: print( " ",end="") print("") # Driver program n = 9 pattern(n) # This code is contributed by Smitha. C# // C# code to print the above // specified pattern using System; public class GFG { // Function to print pattern static void pattern(int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == 0 || i == n - 1 || j == 0 || j == n - 1 || i == j || i == n - 1 - j) Console.Write("#"); else Console.Write(" "); } Console.WriteLine(); } } // Driver code public static void Main() { int n = 9; pattern(n); } } // This code is contributed by Sam007. PHP <?php // PHP code to print the above // specified pattern // Function to print pattern function pattern($n) { for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) { if ($i == 0 || $i == $n - 1 || $j == 0 || $j == $n - 1 || $i == $j || $i == $n - 1 - $j) echo "#"; else echo " "; } echo "\n"; } } // Driver Code $n = 9; pattern($n); // This code is contributed by nitin mittal ?> JavaScript <script> // JavaScript code to print the above // specified pattern // Function to print pattern function pattern(n) { for (var i = 0; i < n; i++) { for (var j = 0; j < n; j++) { if ( i === 0 || i === n - 1 || j === 0 || j === n - 1 || i === j || i === n - 1 - j ) document.write("#"); else document.write(" "); } document.write("<br>"); } } // Driver code var n = 9; pattern(n); </script> Output: ######### ## ## # # # # # # # # # # # # # # # # # # # ## ## ######### Time complexity: O(n^2) for given nAuxiliary space: O(1) Comment More infoAdvertise with us Next Article Pattern to print X in a rectangular box N nikki96 Follow Improve Article Tags : Mathematical DSA Practice Tags : Mathematical Similar Reads Print rectangular pattern with given center Given 3 positive integer c1, c2 and n, where n is size of 2-D square matrix. The task is to print the matrix filled with rectangular pattern having center coordinates c1, c2 such that 0 <= c1, c2 < n. Examples: Input: c1 = 2, c2 = 2, n = 5Output:2 2 2 2 2 2 1 1 1 2 2 1 0 1 2 2 1 1 1 2 2 2 2 2 4 min read 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 4 min read Print concentric rectangular pattern in a 2d matrix Given a positive integer n, print the matrix filled with rectangle pattern as shown below: a a a a a a b b b a a b c b a a b b b a a a a a a where a = n, b = n - 1,c = n - 2 and so on. Examples: Input : n = 4 Output : 4 4 4 4 4 4 4 4 3 3 3 3 3 4 4 3 2 2 2 3 4 4 3 2 1 2 3 4 4 3 2 2 2 3 4 4 3 3 3 3 3 13 min read Print the Alphabets A to Z in Star Pattern Given any alphabet between A to Z, the task is to print the pattern of the given alphabet using star. Examples: Input: A Output: ** * * ****** * * * * Input: P Output: ***** * * ***** * * Approach: The code to print each alphabet is created in a separate function. Use switch statement to call the de 15+ min read Inner reducing pattern printing Given a number N, print the following pattern. Examples : Input : 4 Output : 4444444 4333334 4322234 4321234 4322234 4333334 4444444 Explanation: (1) Given value of n forms the outer-most rectangular box layer. (2) Value of n reduces by 1 and forms an inner rectangular box layer. (3) The step (2) is 5 min read Like