📜  在矩形框中打印 X 的图案

📅  最后修改于: 2022-05-13 01:57:59.193000             🧑  作者: Mango

在矩形框中打印 X 的图案

给定长度值,使用 # 和“”在框中打印 X 模式
例子:

Input  :   10 
Output :   ##########
           ##      ##
           # #    # #
           #  #  #  #
           #   ##   #
           #   ##   #
           #  #  #  #
           # #    # #
           ##      ##
           ##########

Input  :   7 
Output :   #######
           ##   ##
           # # # #  
           #  #  #
           # # # #
           ##   ##
           #######

下面是以矩形框模式打印 X 的实现:

C++
// CPP code to print the above
// specified pattern
#include 
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
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


Javascript


输出:
#########
##     ##
# #   # #
#  # #  #
#   #   #
#  # #  #
# #   # #
##     ##
#########