Print Plus Pattern in Java
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.
Java Program
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.
- 1
n / 2(integer division) locates the grid's middle index — row 2 and column 2 for a 5-wide grid. - 2
row == n / 2marks every position in the middle row;col == n / 2marks every position in the middle column. - 3A star is printed if either condition holds; every other position prints a space.
- 4Both loops run
0ton - 1, visiting every cell of the square grid exactly once.
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.
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
Approach 2: Java 8
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.
- 1
IntStream.range(0, n)generates one stream element per row. - 2For each row, an inner
IntStream.range(0, n)generates that row's column indices. - 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
Collectors.joining("")concatenates that row's characters with no separator, matching the loop version's plain character append.
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.
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.