The window star pattern draws a square frame divided into four panes by a cross through the middle, like a window seen from outside.
Quick answer: Use a (2n + 1) × (2n + 1) grid. Print a star when the cell is on the first, middle or last row, or on the first, middle or last column. The middle row and column are n + 1.
This program is part of the Star Patterns collection.
Table of contents8 sections
Window Star Pattern output
Output (n = 3):
******* * * * * * * ******* * * * * * * *******

How the window star pattern works
The window is six straight lines: three across and three down. Every line is a single row or column number, so the whole picture is one condition with six parts joined by ||. An odd grid size, 2n + 1, gives a single middle row and column, so the cross sits exactly in the centre and the four panes are the same size.
- Run the outer loop for
ifrom1to2 × n + 1, one row per pass. - Go through
2 × n + 1positions in the row. Print stars wherei = 1 or i = n + 1 or i = 2 × n + 1 or j = 1 or j = n + 1 or j = 2 × n + 1; print spaces everywhere else. - Move to the next line.
Key takeaways
- The window is three rows and three columns of stars.
- An odd size
2n + 1puts the cross exactly in the middle. - Each pane is
n − 1cells wide and tall. - Drop the middle row and column and you get a plain hollow square.
Window Star Pattern in C
#include <stdio.h>
int main(void) {
int n = 3;
for (int i = 1; i <= 2 * n + 1; i++) {
for (int j = 1; j <= 2 * n + 1; j++) {
if (i == 1 || i == n + 1 || i == 2 * n + 1 || j == 1 || j == n + 1 || j == 2 * n + 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}Window Star Pattern in C++
#include <iostream>
using namespace std;
int main() {
int n = 3;
for (int i = 1; i <= 2 * n + 1; i++) {
for (int j = 1; j <= 2 * n + 1; j++) {
if (i == 1 || i == n + 1 || i == 2 * n + 1 || j == 1 || j == n + 1 || j == 2 * n + 1)
cout << "*";
else
cout << " ";
}
cout << endl;
}
return 0;
}Window Star Pattern in Java
public class WindowStar {
public static void main(String[] args) {
int n = 3;
for (int i = 1; i <= 2 * n + 1; i++) {
for (int j = 1; j <= 2 * n + 1; j++) {
if (i == 1 || i == n + 1 || i == 2 * n + 1 || j == 1 || j == n + 1 || j == 2 * n + 1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}Window Star Pattern in Python
n = 3
for i in range(1, 2 * n + 2):
for j in range(1, 2 * n + 2):
if i == 1 or i == n + 1 or i == 2 * n + 1 or j == 1 or j == n + 1 or j == 2 * n + 1:
print("*", end="")
else:
print(" ", end="")
print()Time and space complexity
The program prints one character for each position in the pattern, so for n rows it runs in O(n²) time. It only needs a few loop variables, so the extra memory is O(1).
Frequently asked questions
How do I make more panes?
Add more lines: for a 3 × 3 window use the rows and columns 1, n + 1, 2n + 1, 3n + 1 on a grid of size 3n + 1.
How is it different from the plus sign pattern?
The plus sign only has the middle row and column. The window adds the outer frame, which closes the four panes.
How do I let the user choose the size?
Read n from the keyboard instead of fixing it in the code: scanf("%d", &n); in C, cin >> n; in C++, new Scanner(System.in).nextInt() in Java and int(input()) in Python.











