Pattern to print X in a rectangular box

21 May, 2021
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++
Java
Python 3
C#
PHP
Javascript
// CPP code to print the above// specified pattern#include <bits/stdc++.h>using namespace std; // Function to print patternvoid 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 programint main(){    int n = 9;    pattern(n);    return 0;}
// Java code to print the above// specified patternclass 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
# Python3 code to print the above# specified pattern # Function to print patterndef 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 programn = 9pattern(n) # This code is contributed by Smitha.
// C# code to print the above// specified patternusing 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 code to print the above// specified pattern // Function to print patternfunction 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?>
<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>
#########
##     ##
# #   # #
#  # #  #
#   #   #
#  # #  #
# #   # #
##     ##
#########
 
Sam007
Smitha Dinesh Semwal
nitin mittal
rdtank
pattern-printing
School Programming
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Constructors in Java
Exceptions in Java
Python Exception Handling
Python Try Except
Ternary Operator in Python
How JVM Works - JVM Architecture?
Python program to add two numbers
Variables in Java
Data types in Java
Difference between Abstract Class and Interface in Java