The Wayback Machine - https://web.archive.org/web/20240712025907/https://www.geeksforgeeks.org/program-print-diagonal-star-patterns/
Open In App

Program to print diagonal star patterns

Last Updated : 20 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




<?php
// php program to print 
// diagonal star patterns
  
function pattern($n)
{
      
    // Loop denoting rows
    for ($i = 0; $i < $n; $i++)
    {
          
        // Loop denoting columns
        for ($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)
                echo "*";
            else
                echo " ";
        }
        echo "\n";
    }
}
  
    // Driver code
    // n denotes size which should be odd
    $n = 7;
      
    // Function calling
    pattern($n);
  
// This code is contributed by mits
?>


Javascript




<script>
// Javascript program to print diagonal star patterns
 function pattern( n) 
 {
   
        // Loop denoting rows
        for (let i = 0; i < n; i++) {
  
            // Loop denoting columns
            for (let 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)
                    document.write("*");
                else
                    document.write("  ");
            }
            document.write("<br/>");
        }
    }
  
    // Driver function
  
    // n denotes size which should be odd
    let n = 7;
  
    // Function calling
    pattern(n);
  
// This code is contributed by gauravrajput1 
</script>


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




<?php
// php program to print
// diagonal pattern
  
function pattern($n)
{
      
    // For printing upper portion
    $c1 = floor(($n - 1) / 2);
      
    // For printing lower portion
    $c2 = floor(3 * $n / 2 - 1);
      
    // Loop denoting rows
    for ($i = 0; $i < $n; $i++)
    {
          
        // Loop denoting columns
        for ($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)
                echo "*";
            else
                echo " ";
        }
        echo "\n";
    }
}
  
    // Driver code
    // n denotes size
    $n = 9;
      
    // Function calling
    pattern($n);
      
// This code is contributed by mits
?>


Javascript




<script>
// javascript program to print diagonal star patterns
  
  
    function pattern(n) {
        // For printing upper portion
        var c1 = (n - 1) / 2;
  
        // For printing lower portion
        var c2 = parseInt(3 * n / 2 )- 1;
  
        // Loop denoting rows
        for (var i = 0; i < n; i++) {
  
            // Loop denoting columns
            for (var 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)
                    document.write(" *");
                else
                    document.write("   ");
            }
            document.write("<br/>");
        }
    }
  
    // Driver function
        // n denotes size which should be odd
        var n = 9;
  
        // Function calling
        pattern(n);
  
// This code contributed by gauravrajput1 
</script>


Output : 
 

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

Time Complexity: O(n2)

Auxiliary Space: O(1)



Similar Reads

Program to print hollow rectangle or square star patterns
Hollow rectangle star pattern : The task is print below hollow pattern of given dimension. ******************** * * * * * * * * ******************** Explanation: Input number of rows and columns.For rows of rectangle run the outer loop from 1 to rows.for (i = 1; i &lt; = rows; i++)For column of rectangle run the inner loop from 1 to columns. for (j
4 min read
Print different star patterns in SQL
Let's see how we can print the pattern of various type using SQL. Syntax : Declare @variable_name DATATYPE -- first declare all the -- variables with datatype -- like (int) select @variable = WITH_ANY_VALUE -- select the variable and -- initialize with value while CONDITION -- condition like @variable &gt; 0 begin -- begin print replicate('*', @var
2 min read
Program to swap upper diagonal elements with lower diagonal elements of matrix.
Given a square matrix, swap upper diagonal elements of matrix with lower diagonal elements of matrix. Examples : Input: 2 3 5 6 4 5 7 9 8 6 4 9 1 3 5 6 Output: 2 4 8 1 3 5 6 3 5 7 4 5 6 9 9 6 Input: 1 2 3 4 5 6 7 8 9 Output: 1 4 7 2 5 8 3 6 9Recommended PracticeSwapping TrianglesTry It! Below is the implementation of above idea : C/C++ Code // CPP
7 min read
Length of a Diagonal of a Parallelogram using the length of Sides and the other Diagonal
Given two integers A and B, denoting the length of a parallelogram and an integer D, denoting the length of a diagonal, the task is to find the length of another diagonal of the parallelogram. Examples: Input: A = 10, B = 30, D = 20Output: 40.0 Input: A = 6, B = 8, D = 10Output: 10.0 Approach: The relation between sides and diagonals of a parallelo
3 min read
Maximum sum of elements in a diagonal parallel to the main diagonal of a given Matrix
Give a square matrix mat[][] of dimensions N * N, the task is to find the maximum sum of elements present in the given matrix along the diagonals which are parallel to the main diagonal. Below is the image of the same. Examples: Input: mat[][] = {{1, 2, 5, 7}, {2, 6, 7, 3}, {12, 3, 2, 4}, {3, 6, 9, 4}}Output: 18Explanation:Sum of elements present i
11 min read
Program to print number with star pattern
We have to print the pattern as given in the below example.Examples : Input : 5 Output : 1 1*2 1*2*3 1*2 1 Input : 9 Output : 1 1*2 1*2*3 1*2*3*4 1*2*3*4*5 1*2*3*4 1*2*3 1*2 1 C/C++ Code #include &lt;iostream&gt; using namespace std; // C++ program to print above pattern void display(int n) { // 'sp' used for space and 'st' used for star int sp = n
6 min read
Program to print half Diamond star pattern
Given an integer N, the task is to print half-diamond-star pattern. ************************************ Examples: Input: N = 3 Output: * ** *** ** * Input: N = 6 Output: * ** *** **** ***** ****** ***** **** *** ** * Approach: The idea is to break the pattern into two halves that is upper half and lower half. Then print them separately with the he
4 min read
Program to Print Inverted Left Half Pyramid Pattern (Star Pattern)
Given an integer N, the task is is to print a left half pyramid pattern with N rows. In this pattern, the first row contains N stars, the second row contains N - 1 stars, and so forth until the Nth row, which contains only 1 star. All the stars are aligned to the right. Examples: Input: 3Output: *** ** * Input: 5Output: ***** **** *** ** * Approach
4 min read
Program to Print Inverted Right Half Pyramid Pattern (Star Pattern)
Given an integer N, print N rows of inverted right half pyramid pattern. In inverted right half pattern of N rows, the first row has N number of stars, second row has (N - 1) number of stars and so on till the Nth row which has only 1 star. Examples: Input: n = 5Output:*************** Input: n = 3Output:****** Approach: The problem can be solved us
3 min read
Program to Print Right Half Pyramid Pattern (Star Pattern)
Given an integer N, print N rows of right half pyramid pattern. In right half pattern of N rows, the first row has 1 star, second row has 2 stars and so on till the Nth row which has N stars. All the stars are left aligned. Examples: Input: 3Output: ****** Input: 5Output: *************** Approach: The problem can be solved using two nested loops. T
3 min read
three90RightbarBannerImg