# Print window pattern

The "Print Window Pattern" is a programming problem that involves printing a specific pattern of asterisks (*) and spaces to resemble a window. The pattern is constructed based on the height provided as input. The code provided is a C program that solves this problem and generates the desired pattern.

## Problem Statement

The goal is to create a pattern that resembles a window, where the outer frame consists of asterisks and the inner space is left empty. The pattern should have a specific height, which determines the number of rows and columns.

For example, if the height is 5, the pattern would look like this:

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

If the height is 9, the pattern would look like this:

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

The goal is to generate this pattern dynamically based on the input height, making it adaptable to different sizes.

## Algorithm and Pseudocode

The algorithm to solve this problem is as follows:

1. Define a function called `printPattern` that takes the `height` as a parameter.
2. Print the value of `height` to indicate the current pattern's height.
3. Calculate the midpoint by dividing the `height` by 2.
4. Iterate through each row and column using nested `for` loops.
5. At each position, check if the current row or column is at the boundary of the pattern or at the midpoint.
6. If any of the above conditions are met, print an asterisk (*), indicating the outer frame of the window.
7. Otherwise, print a space, indicating the inner space of the window.
8. Print a newline character after each row is completed.
9. Repeat until all rows and columns have been processed.
10. End the function.
11. In the `main` function, call the `printPattern` function with different height values to test and generate the desired patterns.

The pseudocode representation of the algorithm:

``````
function printPattern(height):
print "Height: ", height
mid = height / 2
for i from 0 to height:
for j from 0 to height:
if (i == 0 or i + 1 == height or j == 0 or j + 1 == height or i == mid or j == mid or (height % 2 == 0 and (i + 1 == mid or j + 1 == mid))):
print "* "
else:
print "  "
print newline
end function

function main:
printPattern(5)
printPattern(9)
printPattern(7)
printPattern(8)
end function```
```

## Code Solution

``````//  C program for
//  Print window pattern
#include <stdio.h>

void printPattern(int height)
{
printf("\n Height : %d  \n\n", height);
// When height is Even
int mid = height / 2;
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < height; ++j)
{
if (i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(i == mid) || (j == mid) ||
(height % 2 == 0 && (i + 1 == mid || j + 1 == mid)))
{
// Include star
printf("* ");
}
else
{
printf("  ");
}
}
printf("\n");
}
}
int main(int argc, char
const *argv[])
{
// Test
printPattern(5);
printPattern(9);
printPattern(7);
printPattern(8);
return 0;
}``````

#### Output

`````` Height : 5

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

Height : 9

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

Height : 7

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

Height : 8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *``````
``````/*
Java Program
Print window pattern
*/
public class Pattern
{
public void printPattern(int height)
{
System.out.println("\n Height : " + height + " \n");
// When height is Even
int mid = height / 2;
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < height; ++j)
{
if (i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(i == mid) || (j == mid) ||
(height % 2 == 0 && (i + 1 == mid || j + 1 == mid)))
{
// Include star
System.out.print("* ");
}
else
{
System.out.print("  ");
}
}
System.out.print("\n");
}
}
public static void main(String[] args)
{
// Test
}
}``````

#### Output

`````` Height : 5

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

Height : 9

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

Height : 7

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

Height : 8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Print window pattern
*/
class Pattern
{
public: void printPattern(int height)
{
cout << "\n Height : " << height << " \n" << endl;
// When height is Even
int mid = height / 2;
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < height; ++j)
{
if (i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(i == mid) || (j == mid) ||
(height % 2 == 0 && (i + 1 == mid || j + 1 == mid)))
{
// Include star
cout << "* ";
}
else
{
cout << "  ";
}
}
cout << "\n";
}
}
};
int main()
{
// Test
return 0;
}``````

#### Output

`````` Height : 5

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

Height : 9

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

Height : 7

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

Height : 8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *``````
``````// Include namespace system
using System;
/*
Csharp Program
Print window pattern
*/
public class Pattern
{
public void printPattern(int height)
{
Console.WriteLine("\n Height : " + height + " \n");
// When height is Even
int mid = height / 2;
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < height; ++j)
{
if (i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(i == mid) || (j == mid) ||
(height % 2 == 0 && (i + 1 == mid || j + 1 == mid)))
{
// Include star
Console.Write("* ");
}
else
{
Console.Write("  ");
}
}
Console.Write("\n");
}
}
public static void Main(String[] args)
{
// Test
}
}``````

#### Output

`````` Height : 5

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

Height : 9

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

Height : 7

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

Height : 8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *``````
``````package main
import "fmt"
/*
Go Program
Print window pattern
*/
type Pattern struct {}
func getPattern() * Pattern {
var me *Pattern = &Pattern {}
return me
}
func(this Pattern) printPattern(height int) {
fmt.Println("\n Height : ", height, " \n")
// When height is Even
var mid int = height / 2
for i := 0 ; i < height ; i++ {
for j := 0 ; j < height ; j++ {
if i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(i == mid) || (j == mid) ||
(height % 2 == 0 && (i + 1 == mid || j + 1 == mid)) {
// Include star
fmt.Print("* ")
} else {
fmt.Print("  ")
}
}
fmt.Print("\n")
}
}
func main() {
var task * Pattern = getPattern()
// Test
}``````

#### Output

`````` Height : 5

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

Height : 9

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

Height : 7

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

Height : 8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *``````
``````<?php
/*
Php Program
Print window pattern
*/
class Pattern
{
public	function printPattern(\$height)
{
echo("\n Height : ".\$height." \n\n");
// When height is Even
\$mid = (int)(\$height / 2);
for (\$i = 0; \$i < \$height; ++\$i)
{
for (\$j = 0; \$j < \$height; ++\$j)
{
if (\$i == 0 || \$i + 1 == \$height ||
\$j == 0 || \$j + 1 == \$height ||
(\$i == \$mid) || (\$j == \$mid) ||
(\$height % 2 == 0 && (\$i + 1 == \$mid || \$j + 1 == \$mid)))
{
// Include star
echo("* ");
}
else
{
echo("  ");
}
}
echo("\n");
}
}
}

function main()
{
// Test
}
main();``````

#### Output

`````` Height : 5

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

Height : 9

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

Height : 7

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

Height : 8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *``````
``````/*
Node JS Program
Print window pattern
*/
class Pattern
{
printPattern(height)
{
console.log("\n Height : " + height + " \n");
// When height is Even
var mid = parseInt(height / 2);
for (var i = 0; i < height; ++i)
{
for (var j = 0; j < height; ++j)
{
if (i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(i == mid) || (j == mid) ||
(height % 2 == 0 && (i + 1 == mid || j + 1 == mid)))
{
// Include star
process.stdout.write("* ");
}
else
{
process.stdout.write("  ");
}
}
process.stdout.write("\n");
}
}
}

function main()
{
// Test
}
main();``````

#### Output

`````` Height : 5

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

Height : 9

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

Height : 7

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

Height : 8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *``````
``````#    Python 3 Program
#    Print window pattern
class Pattern :
def printPattern(self, height) :
print("\n Height : ", height ," \n")
#  When height is Even
mid = int(height / 2)
i = 0
while (i < height) :
j = 0
while (j < height) :
if (i == 0 or i + 1 == height or
j == 0 or j + 1 == height or
(i == mid) or(j == mid) or
(height % 2 == 0 and(i + 1 == mid or j + 1 == mid))) :
#  Include star
print("* ", end = "")
else :
print("  ", end = "")

j += 1

print(end = "\n")
i += 1

def main() :
#  Test

if __name__ == "__main__": main()``````

#### Output

`````` Height :  5

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

Height :  9

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

Height :  7

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

Height :  8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *``````
``````#    Ruby Program
#    Print window pattern
class Pattern
def printPattern(height)
print("\n Height : ", height ," \n", "\n")
#  When height is Even
mid = height / 2
i = 0
while (i < height)
j = 0
while (j < height)
if (i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(i == mid) || (j == mid) ||
(height % 2 == 0 && (i + 1 == mid || j + 1 == mid)))
#  Include star
print("* ")
else

print("  ")
end

j += 1
end

print("\n")
i += 1
end

end

end

def main()
#  Test
end

main()``````

#### Output

`````` Height : 5

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

Height : 9

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

Height : 7

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

Height : 8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
``````
``````/*
Scala Program
Print window pattern
*/
class Pattern()
{
def printPattern(height: Int): Unit = {
println("\n Height : " + height + " \n");
// When height is Even
var mid: Int = height / 2;
var i: Int = 0;
while (i < height)
{
var j: Int = 0;
while (j < height)
{
if (i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(i == mid) || (j == mid) ||
(height % 2 == 0 && (i + 1 == mid || j + 1 == mid)))
{
// Include star
print("* ");
}
else
{
print("  ");
}
j += 1;
}
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Pattern = new Pattern();
// Test
}
}``````

#### Output

`````` Height : 5

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

Height : 9

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

Height : 7

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

Height : 8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *``````
``````/*
Swift 4 Program
Print window pattern
*/
class Pattern
{
func printPattern(_ height: Int)
{
print("\n Height : ", height ," \n");
// When height is Even
let mid: Int = height / 2;
var i: Int = 0;
while (i < height)
{
var j: Int = 0;
while (j < height)
{
if (i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(i == mid) || (j == mid) ||
(height % 2 == 0 && (i + 1 == mid || j + 1 == mid)))
{
// Include star
print("* ", terminator: "");
}
else
{
print("  ", terminator: "");
}
j += 1;
}
print(terminator: "\n");
i += 1;
}
}
}
func main()
{
// Test
}
main();``````

#### Output

`````` Height :  5

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

Height :  9

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

Height :  7

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

Height :  8

* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *
* * * * * * * *
*     * *     *
*     * *     *
* * * * * * * *``````
``````/*
Kotlin Program
Print window pattern
*/
class Pattern
{
fun printPattern(height: Int): Unit
{
println("\n Height : " + height + " \n");
// When height is Even
val mid: Int = height / 2;
var i: Int = 0;
while (i < height)
{
var j: Int = 0;
while (j < height)
{
if (i == 0 || i + 1 == height ||
j == 0 || j + 1 == height ||
(i == mid) || (j == mid) ||
(height % 2 == 0 && (i + 1 == mid || j + 1 == mid)))
{
// Include star
print("* ");
}
else
{
print("  ");
}
j += 1;
}
print("\n");
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
// Test
}``````

#### Output

`````` Height : 5

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

Height : 9

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

Height : 7

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

Height : 8

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

## Output Explanation

The code provided generates the following output:

Height: 5

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

Height: 9

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

Height: 7

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

Height: 8

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

## Time Complexity

The time complexity of the given code is O(n^2), where n is the height of the pattern. This is because there are nested loops iterating from 0 to the height value, resulting in a quadratic relationship between the input size and the number of operations performed.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.