How do I print a butterfly pattern using loops in Java?

You can print a butterfly pattern by placing two groups of stars on each row with a gap between them. Grow the groups toward the middle of the pattern, then shrink them again to create mirrored wings.

Source code

Save this example as ButterflyPatternExample.java. It uses a Java 25 compact source file with void main() and the IO console methods. Use JDK 25 or later; no preview flags or external libraries are required.

void main() {
    int halfHeight = 5;

    for (int row = 1; row <= halfHeight; row++) {
        for (int star = 0; star < row; star++) {
            IO.print("*");
        }
        for (int space = 0; space < 2 * (halfHeight - row); space++) {
            IO.print(" ");
        }
        for (int star = 0; star < row; star++) {
            IO.print("*");
        }
        IO.println();
    }

    for (int row = halfHeight - 1; row >= 1; row--) {
        for (int star = 0; star < row; star++) {
            IO.print("*");
        }
        for (int space = 0; space < 2 * (halfHeight - row); space++) {
            IO.print(" ");
        }
        for (int star = 0; star < row; star++) {
            IO.print("*");
        }
        IO.println();
    }
}

Explanation

The variable halfHeight controls the number of rows from the top edge through the center, including the center row. For a positive value n, the complete pattern has 2 * n - 1 rows and a width of 2 * n characters.

The first outer loop counts from 1 through halfHeight. Each row contains three parts, printed by three inner loops in sequence:

  1. row stars for the left wing.
  2. 2 * (halfHeight - row) spaces for the middle gap.
  3. row stars for the right wing.

With halfHeight = 5, the upper half uses these counts:

Row counter Left stars Middle spaces Right stars
1 1 8 1
2 2 6 2
3 3 4 3
4 4 2 4
5 5 0 5

Each step adds one star to each wing and removes two spaces from the gap. The total width stays constant because row + 2 * (halfHeight - row) + row simplifies to 2 * halfHeight.

On the center row, the gap contains zero spaces. The middle loop does not execute, so the two groups meet and produce a solid line of ten stars.

The second outer loop starts at halfHeight - 1 and counts down to 1. It uses the same three inner loops, shrinking the wings and widening the gap. Starting one step below halfHeight prints the center row only once and mirrors the rows above it.

Each star and space occupies one character position. IO.print() writes the three parts on the current line, while IO.println() starts the next row. The IO class belongs to java.lang, so no explicit import is needed.

Run the example

With JDK 25 or later installed, open a terminal in the directory containing the source file and run it directly:

java ButterflyPatternExample.java

You can also compile the source file explicitly and then run the compiled program:

javac ButterflyPatternExample.java
java ButterflyPatternExample

Output

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

Display the output in a monospaced font to preserve alignment. The spaces between the wings are part of the pattern.

Try it yourself

Change halfHeight to 3, then to 7. Before running each version, calculate the total number of rows, the width, and the gap on the first row. For a positive value n, these are 2 * n - 1, 2 * n, and 2 * (n - 1), respectively.

Try halfHeight = 1. The first outer loop prints two adjacent stars, and the second outer loop does not execute. With a value of zero, neither outer loop executes and the program prints nothing. Use positive values to produce a visible pattern.

Change the second outer loop to start at row = halfHeight. This repeats the solid center row and produces a pattern with 2 * halfHeight rows instead.

Finally, compare the changing middle gap with the changing leading spaces in How do I print an hourglass pattern using loops in Java?. In both examples, the spaces are calculated from the current row counter.

Leave a Reply

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