Java ProgramsPatternsPrint Plus Pattern

Print Plus Pattern in Java

intermediate·  Patterns  ·  Star Patterns

Problem

A plus sign is traced by a square grid's exact middle row and middle column at once, the same two-line idea as the X pattern but aligned to the grid's axes instead of its diagonals.

Given a grid size n, print a plus shape made of stars where the middle row and middle column of the grid meet.

Input
5
Output
* * ***** * *

Java Program

Java
public class PlusPattern { public static void main(String[] args) { int n = 5; for (int row = 0; row < n; row++) { StringBuilder line = new StringBuilder(); for (int col = 0; col < n; col++) { if (row == n / 2 || col == n / 2) { // on the middle row or the middle column line.append('*'); } else { line.append(' '); } } System.out.println(line); } } }

Output

* * ***** * *

Core Logic

Checking whether each grid position sits in the middle row or the middle column, and printing a star only there, traces both center lines in a single sweep of the grid.

How It Works
  1. 1n / 2 (integer division) locates the grid's middle index — row 2 and column 2 for a 5-wide grid.
  2. 2row == n / 2 marks every position in the middle row; col == n / 2 marks every position in the middle column.
  3. 3A star is printed if either condition holds; every other position prints a space.
  4. 4Both loops run 0 to n - 1, visiting every cell of the square grid exactly once.
In the middle row (row = 2), every column satisfies row == n / 2, so the entire row fills with stars: *****.
💡

Key Point: Using integer division for n / 2 is what picks a single, well-defined middle index — this pattern reads cleanest with an odd n, where that middle index sits exactly in the center.

Complexity
Time Complexity: O(n²)Space Complexity: O(1)

Why: The nested loop evaluates the middle-row/column conditions once per grid cell, a total proportional to n², with no storage beyond the loop counters and a per-row StringBuilder.

Key Concepts

nested for loopmiddle row/column condition2D grid

Approach 2: Java 8

Java
import java.util.stream.Collectors; import java.util.stream.IntStream; public class PlusPatternStream { public static void main(String[] args) { int n = 5; IntStream.range(0, n) .mapToObj(row -> IntStream.range(0, n) .mapToObj(col -> (row == n / 2 || col == n / 2) ? "*" : " ") .collect(Collectors.joining(""))) .forEach(System.out::println); } }

Output

* * ***** * *

Core Logic

Since each position's middle-row-or-column status depends only on its own row and column, mapping every column index directly to that test traces both center lines without visiting neighboring cells.

How It Works
  1. 1IntStream.range(0, n) generates one stream element per row.
  2. 2For each row, an inner IntStream.range(0, n) generates that row's column indices.
  3. 3.mapToObj(col -> (row == n / 2 || col == n / 2) ? "*" : " ") tests the same middle-row and middle-column conditions the loop version does, per position.
  4. 4Collectors.joining("") concatenates that row's characters with no separator, matching the loop version's plain character append.
In the middle row (row = 2 for n = 5), every column satisfies row == n / 2, so every position in that row's stream maps to "*".
💡

Key Point: Each condition reads only its own row and col, so it maps onto a stream exactly as cleanly as it fits inside a nested loop — no dependency on any other cell's result.

Complexity
Time Complexity: O(n²)Space Complexity: O(1)

Why: The nested streams still evaluate the middle-row/column conditions once per grid cell, a total proportional to n², without collecting the full grid.

Key Concepts

StreamIntStream.range()Collectors.joining()

Related Programs