Program to print window pattern
Print the pattern in which there is a hollow square and plus sign inside it. Pattern will be as per the n i.e. number of rows given as shown in 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 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.
Python3
# Python 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) |
chevron_right
filter_none
Output :
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Time complexity: O(n^2)
Recommended Posts:
- Program to print the given H Pattern
- Program to print the pattern 'G'
- Program to print pattern
- Program to print the given Z Pattern
- Program to print the pattern "GFG"
- Program to print the pattern ‘D’
- Program to print a rectangle pattern
- Program to print circle pattern
- Program to print pyramid pattern
- Program to print Hut Star pattern
- Program to print Crown Pattern
- C++ Program to Print Cross or X Pattern
- Program to print interesting pattern
- Program to print the Ladder Pattern
- Program to print Kite Pattern
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

