Pattern to print X in a rectangular box
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; } |
chevron_right
filter_none
Java
// Java code to print the above // specified pattern 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 |
chevron_right
filter_none
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 = 9pattern(n) # This code is contributed by Smitha. |
chevron_right
filter_none
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. |
chevron_right
filter_none
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 ?> |
chevron_right
filter_none
Output:
######### ## ## # # # # # # # # # # # # # # # # # # # ## ## #########
Recommended Posts:
- Print concentric rectangular pattern in a 2d matrix
- Print the following pyramid pattern
- Program to print the given H Pattern
- Program to print the pattern "GFG"
- Program to print the pattern 'G'
- Program to print pattern
- Program to print the given Z Pattern
- Print the given pattern recursively
- Print a pattern without using any loop
- Program to print the pattern ‘D’
- Program to print a pattern of numbers
- Program to print pyramid pattern
- Program to print pyramid pattern
- Program to print a rectangle pattern
- Program to print numeric pattern
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



