New: 203 pattern programs with tested code in C, C++, Java and Python Explore patterns

Window Star Pattern

Read summary with AI tools

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
  1. Output
  2. How the window star pattern works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Window Star Pattern output

Output (n = 3):

*******
*  *  *
*  *  *
*******
*  *  *
*  *  *
*******
Window Star Pattern layout diagram, row by row (n = 3)
How each row of the window star pattern is built (n = 3). Blue cells are printed characters; grey dots are spaces.

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.

  1. Run the outer loop for i from 1 to 2 × n + 1, one row per pass.
  2. Go through 2 × n + 1 positions in the row. Print stars where i = 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.
  3. Move to the next line.

Key takeaways

  • The window is three rows and three columns of stars.
  • An odd size 2n + 1 puts the cross exactly in the middle.
  • Each pane is n − 1 cells 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.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Latest articles

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.