How do I print a plus sign pattern using loops in Java?

You can print a plus sign by placing stars along the middle row and middle column of a square grid. Use nested for loops to visit each position and an if statement to select the positions that form the sign.

Source code

Save this example as PlusSignPatternExample.java. It uses basic Java syntax and requires no external libraries. Choose a positive odd size to give the pattern a single center position and equal-length arms.

public class PlusSignPatternExample {
    public static void main(String[] args) {
        int size = 7;
        int middle = size / 2;

        for (int row = 0; row < size; row++) {
            for (int column = 0; column < size; column++) {
                if (row == middle || column == middle) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }
}

Explanation

The variable size controls the number of rows and logical columns. Both counters start at zero and stop before size, so a size of seven gives indexes from 0 through 6.

The expression size / 2 calculates the middle index. Because both operands are integers, Java performs integer division. With size = 7, the result is 3. Index 3 has three positions before it and three after it.

The outer loop selects each row, while the inner loop visits every column in that row. The if statement checks two conditions:

Condition Part of the pattern
row == middle The horizontal line across the middle row
column == middle The vertical line down the middle column

The logical OR operator, ||, selects a position when either condition is true. On the middle row, every position contains a star. On every other row, only the middle column contains a star.

At the center, both conditions are true, but the if body executes only once. The center star therefore belongs to both lines without being printed twice.

Each star position prints "* ", and each empty position prints " ". Both strings occupy two character positions, keeping the vertical line aligned. After the inner loop finishes, System.out.println() starts the next row.

Run the example

With a JDK installed, open a terminal in the directory containing the source file and run:

javac PlusSignPatternExample.java
java PlusSignPatternExample

Output

      *
      *
      *
* * * * * * *
      *
      *
      *

Display the output in a monospaced font to preserve alignment. Trailing spaces are omitted from the output shown here; leading spaces are preserved. The apparent proportions depend on the font and character spacing.

Try it yourself

Change size to 5, then to 9. Calculate middle before running each version. For a positive odd size n, the pattern contains 2 * n - 1 stars: n in the horizontal line and n in the vertical line, with the shared center counted once. The example with a size of seven prints 13 stars.

Try size = 6. The program still prints intersecting lines, but the arms are unequal because an even-sized grid has no single middle row or column. Integer division selects index 3, leaving three positions before the intersection and two after it. Use a positive odd size for the symmetric pattern shown above.

With size = 1, the output is a single star. A zero or negative size produces no output because the outer loop never executes.

Finally, replace || with && in the condition. Predict the output: logical AND requires both conditions to be true, so only the center position contains a star. Compare this row-and-column condition with the diagonal conditions in How do I print an X pattern using loops in Java?.

Leave a Reply

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