GeeksforGeeks App
Open App
Browser
Continue

# Program to print diagonal star patterns

Time Complexity: O(N) where N is number of nodes in a given binary tree

Auxiliary Space: O(N)For the given input, this program prints the following pattern. The input must be an odd number.
Examples:

```Input : 7
Output :

*******
**   **
* * * *
*  *  *
* * * *
**   **
*******```

Below is the code printing above pattern :

## 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

 ``

Output :

```    *******
**   **
* * * *
*  *  *
* * * *
**   **
*******
```

Time Complexity: O(n2)

Auxiliary Space: O(1)
For given input, this program prints the following pattern. The input must be an odd number.
Examples :

```Input : 9
Output :

*
* * *
*   *   *
*     *     *
* * * * * * * * *
*     *     *
*   *   *
* * *
*```

Below is the code printing above pattern :

## 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

 ``

Output :

```        *
* * *
*   *   *
*     *     *
* * * * * * * * *
*     *     *
*   *   *
* * *
*
```

Time Complexity: O(n2)

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up