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

Hollow Plus Star Pattern

Read summary with AI tools

The hollow plus star pattern draws the outline of a thick plus sign, the kind used for a first-aid symbol. Each arm is n stars wide and the whole shape fits in a square.

Quick answer: Think of the grid as three bands of rows: the top arm, the middle bar and the bottom arm. In the arms, print the two side edges and the end row. On the two rows where the arms meet the bar, print everything except the gap between the arms. In the bar’s middle rows, print only the far left and right columns.

This program is part of the Star Patterns collection.

Table of contents8 sections
  1. Output
  2. How the hollow plus 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

Hollow Plus Star Pattern output

Output (n = 3):

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

How the hollow plus star pattern works

A thick plus is the middle n columns plus the middle n rows of a (3n − 2)-wide square. Its outline needs a different rule in each band. The arm bands have short vertical edges and a closed end. The two rows where the bar starts and ends draw the shoulders on both sides. The rows inside the bar only show the outer ends. Writing one condition per band, joined with ||, keeps the logic readable.

  1. Run the outer loop for i from 1 to 3 × n − 2, one row per pass.
  2. Go through 3 × n − 2 positions in the row. Print stars where ((i < n or i > 2 × n − 1) and (j = n or j = 2 × n − 1 or ((i = 1 or i = 3 × n − 2) and j ≥ n and j ≤ 2 × n − 1))) or ((i = n or i = 2 × n − 1) and (j ≤ n or j ≥ 2 × n − 1)) or (i > n and i < 2 × n − 1 and (j = 1 or j = 3 × n − 2)); print spaces everywhere else.
  3. Move to the next line.

Key takeaways

  • The plus is the middle columns plus the middle rows of a square grid.
  • Each band of rows has its own simple outline rule.
  • The shoulder rows are where the arms meet the bar.
  • With n = 3 each arm is 3 stars wide and 2 rows long.

Hollow Plus Star Pattern in C

#include <stdio.h>

int main(void) {
    int n = 3;

    for (int i = 1; i <= 3 * n - 2; i++) {
        for (int j = 1; j <= 3 * n - 2; j++) {
            if (((i < n || i > 2 * n - 1) && (j == n || j == 2 * n - 1 || ((i == 1 || i == 3 * n - 2) && j >= n && j <= 2 * n - 1))) || ((i == n || i == 2 * n - 1) && (j <= n || j >= 2 * n - 1)) || (i > n && i < 2 * n - 1 && (j == 1 || j == 3 * n - 2)))
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

Hollow Plus Star Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 3;

    for (int i = 1; i <= 3 * n - 2; i++) {
        for (int j = 1; j <= 3 * n - 2; j++) {
            if (((i < n || i > 2 * n - 1) && (j == n || j == 2 * n - 1 || ((i == 1 || i == 3 * n - 2) && j >= n && j <= 2 * n - 1))) || ((i == n || i == 2 * n - 1) && (j <= n || j >= 2 * n - 1)) || (i > n && i < 2 * n - 1 && (j == 1 || j == 3 * n - 2)))
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}

Hollow Plus Star Pattern in Java

public class HollowPlusStar {
    public static void main(String[] args) {
        int n = 3;

        for (int i = 1; i <= 3 * n - 2; i++) {
            for (int j = 1; j <= 3 * n - 2; j++) {
                if (((i < n || i > 2 * n - 1) && (j == n || j == 2 * n - 1 || ((i == 1 || i == 3 * n - 2) && j >= n && j <= 2 * n - 1))) || ((i == n || i == 2 * n - 1) && (j <= n || j >= 2 * n - 1)) || (i > n && i < 2 * n - 1 && (j == 1 || j == 3 * n - 2)))
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

Hollow Plus Star Pattern in Python

n = 3

for i in range(1, 3 * n - 2 + 1):
    for j in range(1, 3 * n - 2 + 1):
        if ((i < n or i > 2 * n - 1) and (j == n or j == 2 * n - 1 or ((i == 1 or i == 3 * n - 2) and j >= n and j <= 2 * n - 1))) or ((i == n or i == 2 * n - 1) and (j <= n or j >= 2 * n - 1)) or (i > n and i < 2 * n - 1 and (j == 1 or j == 3 * n - 2)):
            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 is this different from the plus sign pattern?

The plus sign pattern draws one line across and one line down. This one draws the outline of a thick plus with a hollow inside.

How do I make a solid thick plus?

Print a star whenever j is between n and 2n − 1 or i is between n and 2n − 1. That fills both bands completely.

Why does the condition look long?

Because it lists the outline band by band. Breaking it into three named booleans, such as inArm, onShoulder and inBar, makes it easier to read in your own code.

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.