Open In App

Program to print window pattern

Last Updated : 13 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Print the pattern in which there is a hollow square and plus sign inside it. The pattern will be as per the n i.e. number of rows given as shown in the example. 

Examples: 

Input : 6
Output : * * * * * *
         *   * *   * 
         * * * * * * 
         * * * * * * 
         *   * *   * 
         * * * * * *

Input : 7
Output : * * * * * * * 
         *     *     * 
         *     *     * 
         * * * * * * * 
         *     *     * 
         *     *     * 
         * * * * * * *

Approach: 

  • We will start a for loop up till n and inside this also there is for loop up till n.
  • In this simply we have to check if the row is first or last or column is first or last, then print “*”.
  • Now we have to check for the middle row and column.
  • So when n is odd, we will have a middle row and column and if row or column is in middle then we will print “*”.
  • If n is even, then rows or columns if equal to these values n/2 and (n/2)+1, then we will print “*”.
  • Else everywhere we have to print ” “(space).

Below is the implementation. 

C++14




// C++ program to print the pattern 
// hollow square with plus inside it
// window pattern
#include <bits/stdc++.h>
using namespace std;
  
// Function to print pattern n means 
// number of rows which we want
void window_pattern (int n)
{
    int c, d;
      
    // If n is odd then we will have
    // only one middle element
    if (n % 2 != 0)
    {
        c = (n / 2) + 1;
        d = 0;
    }
  
    // If n is even then we will have two
    // values
    else
    {
        c = (n / 2) + 1;
        d = n / 2 ;
    }
  
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
           
            // If i,j equals to corner row or
            // column then "*"
            if (i == 1 || j == 1 ||
                i == n || j == n)
                cout << "* ";
  
            else
            {
               
                // If i,j equals to the middle 
                // row or column then  "*"
                if (i == c || j == c)
                    cout << "* ";
  
                else if (i == d || j == d)
                    cout << "* ";
  
                else
                    cout << "  ";
            }
        }
        cout << '\n';
    }
}
  
// Driver Code
int main()
{
    int n = 7;
   
    window_pattern(n);
    return 0;   
}
  
// This code is contributed by himanshu77


Java




// Java program to print the pattern 
// hollow square with plus inside it
// window pattern
class GFG
{
 
  // Function to print pattern n means 
  // number of rows which we want
  static void window_pattern (int n)
  {
    int c, d;
 
    // If n is odd then we will have
    // only one middle element
    if (n % 2 != 0)
    {
      c = (n / 2) + 1;
      d = 0;
    }
 
    // If n is even then we will have
    // two values
    else
    {
      c = (n / 2) + 1;
      d = n / 2 ;
    
    for(int i = 1; i <= n; i++)
    {
      for(int j = 1; j <= n; j++)
      {
 
        // If i,j equals to corner row
        // or column then "*"
        if (i == 1 || j == 1 ||
            i == n || j == n)
          System.out.print("* ");          
        else
        {
 
          // If i,j equals to the middle 
          // row or column then  "*"
          if (i == c || j == c)
            System.out.print("* ");
          else if (i == d || j == d)
            System.out.print("* ");
          else
            System.out.print("  ");
        }
      }
      System.out.println();
    }
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int n = 7;
    window_pattern(n);
  }
}
 
// This code is contributed by divyeshrabadiya07


Python3




# Python3 program to print the pattern
# hollow square with plus inside it
# window pattern
 
 
# function to print pattern n means
# number of rows which we want
def window_pattern(n):
     
    # if n is odd then we will have
    # only one middle element
    if n % 2 != 0:
        c = ( n // 2 ) + 1
        d = 0
         
    # if n is even then we will have two
    # values
    else:
        c = ( n // 2 ) + 1
        d = ( n // 2 )
 
    for i in range( 1 , n + 1 ):
        for j in range( 1 , n + 1 ):
             
            # if i,j equals to corner row or
            # column then "*"
            if i == 1 or j == 1 or i == n or j == n:
                print("*",end=" ")
                 
            else:
                 
                # if i,j equals to the middle row
                # or column then  "*"
                if i == c or j == c:
                    print("*",end=" ")
                     
                elif i == d or j == d:
                    print("*",end=" ")
                 
                else:
                    print(" ",end=" ")
         
        print()
 
 
# Driver Code
if __name__ == "__main__":
    n = 7
    window_pattern(n)


C#




// C# program to print the pattern 
// hollow square with plus inside it
// window pattern
using System;
 
class GFG{
 
// Function to print pattern n means 
// number of rows which we want
static void window_pattern (int n)
{
    int c, d;
     
    // If n is odd then we will have
    // only one middle element
    if (n % 2 != 0)
    {
        c = (n / 2) + 1;
        d = 0;
    }
   
    // If n is even then we will have
    // two values
    else
    {
        c = (n / 2) + 1;
        d = n / 2 ;
    }
   
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
             
            // If i,j equals to corner row
            // or column then "*"
            if (i == 1 || j == 1 ||
                i == n || j == n)
                Console.Write("* ");
                 
            else
            {
                 
                // If i,j equals to the middle 
                // row or column then  "*"
                if (i == c || j == c)
                    Console.Write("* ");
   
                else if (i == d || j == d)
                    Console.Write("* ");
   
                else
                    Console.Write("  ");
            }
        }
        Console.WriteLine();
    }
}
 
// Driver code
static void Main()
{
    int n = 7;
     
    window_pattern(n);
}
}
 
// This code is contributed by divyesh072019


Javascript




<script>
 
      // JavaScript program to
      // print the pattern
      // hollow square with
      // plus inside it
      // window pattern
 
      // Function to print pattern n means
      // number of rows which we want
      function window_pattern(n)
      {
        var c, d;
 
        // If n is odd then we will have
        // only one middle element
        if (n % 2 != 0) {
          c = parseInt(n / 2 + 1);
          d = 0;
        }
 
        // If n is even then we will have two
        // values
        else {
          c = parseInt(n / 2 + 1);
          d = parseInt(n / 2);
        }
 
        for (var i = 1; i <= n; i++) {
          for (var j = 1; j <= n; j++) {
            // If i,j equals to corner row or
            // column then "*"
            if (i == 1 || j == 1 || i == n || j == n)
            document.write("* ");
            else {
              // If i,j equals to the middle
              // row or column then "*"
              if (i == c || j == c)
              document.write("* ");
              else if (i == d || j == d)
              document.write("* ");
              else
              document.write("   ");
            }
          }
          document.write("<br>");
        }
      }
 
      // Driver Code
      var n = 7;
       
      window_pattern(n);
       
</script>


Output :

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

Time complexity: O(n2

Auxiliary space: O(1) because it is using constant space for variables



Previous Article
Next Article

Similar Reads

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
Program to Print Left Half Pyramid Pattern (Star Pattern)
Given an integer N, the task is to print N rows of left half pyramid pattern. In left 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 right aligned. Examples: Input: 3Output: * ***** Input: 5Output: * ** *** ********* Approach: The problem can be solved using t
4 min read
Program to Print Butterfly Pattern (Star Pattern)
Given an integer N, print N rows of Butterfly pattern. Examples: Input: 3Output: * *** ********* *** * Input: 5Output: * *** ***** ******* ***************** ******* ***** *** * Approach: The problem can be solved using three nested loops inside an outer loop. The outer loop will run for the rows, the first inner loop will print the stars, the secon
6 min read
Program to Print Inverted Full Pyramid Pattern (Star Pattern)
Given an integer N, the task is to print a pattern of N rows representing an inverted full pyramid. In this pattern, the first row has (2 * N - 1) stars, the second row has (2 * N - 3) stars, and so on until the Nth row, which has only 1 star. All stars are center-aligned. Examples: Input: 3Output: ***** *** * Input: 5Output: ********* ******* ****
4 min read
Program to Print Full Pyramid Pattern (Star Pattern)
Given an integer N, the task is to print a full pyramid pattern with N rows. In this pattern, each row contains an odd number of stars, ranging from 1 star in the first row to (2 * N - 1) stars in the Nth row. All the stars are center-aligned. Examples: Input: 3Output: * ******** Input: 5Output: * *** ***** **************** Approach: The problem ca
4 min read
PyQtGraph – Getting Window Flags of Plot Window
In this article we will see how we can get window flags of the plot window in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) an
3 min read
PyQtGraph – Setting Window Flag to Plot Window
In this article we will see how we can set window flag to the plot window in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) and
3 min read
Python | Get the smallest window in a string containing all characters of given pattern
Given two strings str and pattern, find the smallest substring in str containing all characters of pattern efficiently. Examples: Input : str = 'geeksforgeeks' pattern = 'gks' Output : geeks Input : str = 'new string' pattern = 'rg' Output : ring Approach #1 : Using Python enumerate() This method uses Python enumerate(). need[k] store how many time
5 min read
three90RightbarBannerImg