Program to print window pattern
Last Updated :
13 Sep, 2022
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
#include <bits/stdc++.h>
using namespace std;
void window_pattern ( int n)
{
int c, d;
if (n % 2 != 0)
{
c = (n / 2) + 1;
d = 0;
}
else
{
c = (n / 2) + 1;
d = n / 2 ;
}
for ( int i = 1; i <= n; i++)
{
for ( int j = 1; j <= n; j++)
{
if (i == 1 || j == 1 ||
i == n || j == n)
cout << "* " ;
else
{
if (i == c || j == c)
cout << "* " ;
else if (i == d || j == d)
cout << "* " ;
else
cout << " " ;
}
}
cout << '\n' ;
}
}
int main()
{
int n = 7;
window_pattern(n);
return 0;
}
|
Java
class GFG
{
static void window_pattern ( int n)
{
int c, d;
if (n % 2 != 0 )
{
c = (n / 2 ) + 1 ;
d = 0 ;
}
else
{
c = (n / 2 ) + 1 ;
d = n / 2 ;
}
for ( int i = 1 ; i <= n; i++)
{
for ( int j = 1 ; j <= n; j++)
{
if (i == 1 || j == 1 ||
i == n || j == n)
System.out.print( "* " );
else
{
if (i == c || j == c)
System.out.print( "* " );
else if (i == d || j == d)
System.out.print( "* " );
else
System.out.print( " " );
}
}
System.out.println();
}
}
public static void main(String[] args)
{
int n = 7 ;
window_pattern(n);
}
}
|
Python3
def window_pattern(n):
if n % 2 ! = 0 :
c = ( n / / 2 ) + 1
d = 0
else :
c = ( n / / 2 ) + 1
d = ( n / / 2 )
for i in range ( 1 , n + 1 ):
for j in range ( 1 , n + 1 ):
if i = = 1 or j = = 1 or i = = n or j = = n:
print ( "*" ,end = " " )
else :
if i = = c or j = = c:
print ( "*" ,end = " " )
elif i = = d or j = = d:
print ( "*" ,end = " " )
else :
print ( " " ,end = " " )
print ()
if __name__ = = "__main__" :
n = 7
window_pattern(n)
|
C#
using System;
class GFG{
static void window_pattern ( int n)
{
int c, d;
if (n % 2 != 0)
{
c = (n / 2) + 1;
d = 0;
}
else
{
c = (n / 2) + 1;
d = n / 2 ;
}
for ( int i = 1; i <= n; i++)
{
for ( int j = 1; j <= n; j++)
{
if (i == 1 || j == 1 ||
i == n || j == n)
Console.Write( "* " );
else
{
if (i == c || j == c)
Console.Write( "* " );
else if (i == d || j == d)
Console.Write( "* " );
else
Console.Write( " " );
}
}
Console.WriteLine();
}
}
static void Main()
{
int n = 7;
window_pattern(n);
}
}
|
Javascript
<script>
function window_pattern(n)
{
var c, d;
if (n % 2 != 0) {
c = parseInt(n / 2 + 1);
d = 0;
}
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 == 1 || j == 1 || i == n || j == n)
document.write( "* " );
else {
if (i == c || j == c)
document.write( "* " );
else if (i == d || j == d)
document.write( "* " );
else
document.write( " " );
}
}
document.write( "<br>" );
}
}
var n = 7;
window_pattern(n);
</script>
|
Output :
* * * * * * *
* * *
* * *
* * * * * * *
* * *
* * *
* * * * * * *
Time complexity: O(n2)
Auxiliary space: O(1) because it is using constant space for variables
Please Login to comment...