📜  打印对角星形图案的程序

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

打印对角星形图案的程序

对于给定的输入,该程序打印以下模式。输入必须是奇数。
例子:

Input : 7
Output :

    *******
    **   **
    * * * *
    *  *  *
    * * * *
    **   **
    *******

下面是上面模式的代码打印:

C++
// CPP program to print diagonal star patterns
#include 
using namespace std;
 
void pattern(int n)
{
    // Loop denoting rows
    for (int i = 0; i < n; i++) {
         
        // Loop denoting columns
        for (int j = 0; j < n; j++) {
             
            // Checking boundary conditions and main
            // diagonal and secondary diagonal conditions
            if (i == 0 || j == 0 || i == j || i == n - 1
                            || j == n - 1 || i + j == n - 1)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }
}
 
// Driver code
int main()
{
    // n denotes size which should be odd
    int n = 7;
    // Function calling
    pattern(n);
    return 0;
}


Java
// Java program to print diagonal star patterns
 
import java.util.*;
import java.lang.*;
 
public class GfG{
    public static void pattern(int n)
    {
    // Loop denoting rows
    for (int i = 0; i < n; i++) {
             
        // Loop denoting columns
        for (int j = 0; j < n; j++) {
             
            // Checking boundary conditions
            // and main diagonal and
            // secondary diagonal conditions
            if (i == 0 || j == 0 || i == j
                || i == n - 1 || j == n - 1
                || i + j == n - 1)
                    System.out.print("*");
            else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
     
    // Driver function
    public static void main(String argc[]){
 
        // n denotes size which should be odd
        int n = 7;
 
        // Function calling
        pattern(n);
    }
     
}
 
// This code is contributed by Sagar Shukla


Python3
# Python 3 program to print
# diagonal star patterns
 
def pattern(n) :
     
    # Loop denoting rows
    for i in range(0 , n) :
 
        # Loop denoting columns
        for j in range(0 , n) :
             
            # Checking boundary conditions and main
            # diagonal and secondary diagonal conditions
            if (i == 0 or j == 0 or i == j 
               or i == n - 1 or j == n - 1
               or i + j == n - 1) :
                print( "*", end="")
            else :
                print(" ",end="")
         
        print("")
     
     
# Driver code
# n denotes size which should be odd
n = 7
 
# Function calling
pattern(n)
 
 
# This code is contributed by Nikita Tiwari.


C#
// C# program to print diagonal
// star patterns
using System;
 
public class GfG{
    public static void pattern(int n)
    {
    // Loop denoting rows
    for (int i = 0; i < n; i++) {
             
        // Loop denoting columns
        for (int j = 0; j < n; j++) {
             
            // Checking boundary conditions,
            // main diagonal and secondary
            // diagonal conditions
            if (i == 0 || j == 0 || i == j
                || i == n - 1 || j == n - 1
                || i + j == n - 1)
                    Console.Write("*");
            else
                    Console.Write(" ");
            }
            Console.WriteLine();
        }
    }
     
    // Driver function
    public static void Main(){
 
        // n denotes size which should be odd
        int n = 7;
 
        // Function calling
        pattern(n);
    }
     
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// CPP program to print diagonal pattern
#include 
using namespace std;
 
void pattern(int n)
{
    // For printing upper portion
    int c1 = (n - 1) / 2;
     
    // For printing lower portion
    int c2 = 3 * n / 2 - 1;
     
    // Loop denoting rows
    for (int i = 0; i < n; i++) {
         
        // Loop denoting columns
        for (int j = 0; j < n; j++) {
             
            // Checking conditions for printing pattern
            if (i + j == c1 || i - j == c1 || j - i == c1
                        || i + j == c2 || i == c1 || j == c1)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }
}
 
// Driver code
int main()
{
    // n denotes size
    int n = 9;
    // Function calling
    pattern(n);
    return 0;
}


Java
// Java program to print diagonal star patterns
 
import java.util.*;
import java.lang.*;
 
public class GfG{
    public static void pattern(int n)
    {
        // For printing upper portion
        int c1 = (n - 1) / 2;
     
        // For printing lower portion
        int c2 = 3 * n / 2 - 1;
     
        // Loop denoting rows
        for (int i = 0; i < n; i++) {
         
        // Loop denoting columns
        for (int j = 0; j < n; j++) {
             
            // Checking conditions for printing
            // pattern
            if (i + j == c1 || i - j == c1
                || j - i == c1 || i + j == c2 ||
                i == c1 || j == c1)
                    System.out.print("*");
            else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
     
    // Driver function
    public static void main(String argc[]){
 
        // n denotes size which should be odd
        int n = 9;
 
        // Function calling
        pattern(n);
    }
     
}
 
// This code is contributed by Sagar Shukla


Python3
# Python 3 program to print
# diagonal pattern
 
def pattern(n) :
     
    # For printing upper portion
    c1 = (n - 1) // 2
     
    # For printing lower portion
    c2 = 3 * n // 2 - 1
     
    # Loop denoting rows
    for i in range(0 , n) :
        # Loop denoting columns
        for j in range(0 , n) :
             
            # Checking conditions for
            # printing pattern
            if (i + j == c1 or i - j == c1 or
                j - i == c1 or i + j == c2 or
                i == c1 or j == c1) :
                print( "*",end = "")
            else :
                print(" ",end = "")
         
        print("")
     
     
 
# Driver code
 
# n denotes size
n = 9
 
# Function calling
pattern(n)
 
# This code is contributed by Nikita Tiwari.


C#
// C# program to print
// diagonal star patterns
using System;
 
class GfG
{
    public static void pattern(int n)
    {
        // For printing
        // upper portion
        int c1 = (n - 1) / 2;
     
        // For printing
        // lower portion
        int c2 = 3 * n / 2 - 1;
     
        // Loop denoting rows
        for (int i = 0; i < n; i++)
        {
         
        // Loop denoting columns
        for (int j = 0; j < n; j++)
        {
             
            // Checking conditions for
            // printing pattern
            if (i + j == c1 || i - j == c1 ||
                j - i == c1 || i + j == c2 ||
                    i == c1 || j == c1)
                    Console.Write("*");
            else
                Console.Write(" ");
        }
            Console.WriteLine();
        }
    }
     
    // Driver Code
    public static void Main()
    {
 
        // n denotes size which
        // should be odd
        int n = 9;
 
        // Function calling
        pattern(n);
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


输出 :

*******
    **   **
    * * * *
    *  *  *
    * * * *
    **   **
    *******
         

对于给定的输入,该程序打印以下模式。输入必须是奇数。
例子 :

Input : 9
Output :


        *
      * * * 
    *   *   * 
  *     *     *
* * * * * * * * *
  *     *     *
    *   *   *
      * * *
        *

下面是上面模式的代码打印:

C++

// CPP program to print diagonal pattern
#include 
using namespace std;
 
void pattern(int n)
{
    // For printing upper portion
    int c1 = (n - 1) / 2;
     
    // For printing lower portion
    int c2 = 3 * n / 2 - 1;
     
    // Loop denoting rows
    for (int i = 0; i < n; i++) {
         
        // Loop denoting columns
        for (int j = 0; j < n; j++) {
             
            // Checking conditions for printing pattern
            if (i + j == c1 || i - j == c1 || j - i == c1
                        || i + j == c2 || i == c1 || j == c1)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }
}
 
// Driver code
int main()
{
    // n denotes size
    int n = 9;
    // Function calling
    pattern(n);
    return 0;
}

Java

// Java program to print diagonal star patterns
 
import java.util.*;
import java.lang.*;
 
public class GfG{
    public static void pattern(int n)
    {
        // For printing upper portion
        int c1 = (n - 1) / 2;
     
        // For printing lower portion
        int c2 = 3 * n / 2 - 1;
     
        // Loop denoting rows
        for (int i = 0; i < n; i++) {
         
        // Loop denoting columns
        for (int j = 0; j < n; j++) {
             
            // Checking conditions for printing
            // pattern
            if (i + j == c1 || i - j == c1
                || j - i == c1 || i + j == c2 ||
                i == c1 || j == c1)
                    System.out.print("*");
            else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
     
    // Driver function
    public static void main(String argc[]){
 
        // n denotes size which should be odd
        int n = 9;
 
        // Function calling
        pattern(n);
    }
     
}
 
// This code is contributed by Sagar Shukla

Python3

# Python 3 program to print
# diagonal pattern
 
def pattern(n) :
     
    # For printing upper portion
    c1 = (n - 1) // 2
     
    # For printing lower portion
    c2 = 3 * n // 2 - 1
     
    # Loop denoting rows
    for i in range(0 , n) :
        # Loop denoting columns
        for j in range(0 , n) :
             
            # Checking conditions for
            # printing pattern
            if (i + j == c1 or i - j == c1 or
                j - i == c1 or i + j == c2 or
                i == c1 or j == c1) :
                print( "*",end = "")
            else :
                print(" ",end = "")
         
        print("")
     
     
 
# Driver code
 
# n denotes size
n = 9
 
# Function calling
pattern(n)
 
# This code is contributed by Nikita Tiwari.

C#

// C# program to print
// diagonal star patterns
using System;
 
class GfG
{
    public static void pattern(int n)
    {
        // For printing
        // upper portion
        int c1 = (n - 1) / 2;
     
        // For printing
        // lower portion
        int c2 = 3 * n / 2 - 1;
     
        // Loop denoting rows
        for (int i = 0; i < n; i++)
        {
         
        // Loop denoting columns
        for (int j = 0; j < n; j++)
        {
             
            // Checking conditions for
            // printing pattern
            if (i + j == c1 || i - j == c1 ||
                j - i == c1 || i + j == c2 ||
                    i == c1 || j == c1)
                    Console.Write("*");
            else
                Console.Write(" ");
        }
            Console.WriteLine();
        }
    }
     
    // Driver Code
    public static void Main()
    {
 
        // n denotes size which
        // should be odd
        int n = 9;
 
        // Function calling
        pattern(n);
    }
}
 
// This code is contributed by anuj_67.

PHP


Javascript


输出 :

*
      * * * 
    *   *   * 
  *     *     *
* * * * * * * * *
  *     *     *
    *   *   *
      * * *
        *