Print Butterfly Star Pattern in Java

Topic

In this program, we will learn how to print a butterfly star pattern using Java. The pattern has a mirror-like shape — the top half looks like a V and the bottom half is its reflection. It's a great example to practice nested loops and symmetry in pattern printing.

Examples

Input: n = 4 (number of rows for each half)


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

Each half of the butterfly is symmetrical. Spaces reduce as stars increase and vice versa.

Interviewer Expectations

The interviewer is checking your understanding of:

  • How to use for loops, especially nested loops.
  • How to print symmetrical shapes using patterns of spaces and stars.
  • How well you structure your loops and maintain readable code.

Approach

We divide the butterfly pattern into two parts:

  1. Top half: From row 1 to n
  2. Bottom half: From row n to 1

Each row has two mirrored star sections, and between them are spaces. The number of stars is equal to the row number, and spaces are calculated as: 2 * (n - row).

Dry Run for n = 3 (Top Half)


Row 1: *    *   → 1 star + 4 spaces + 1 star
Row 2: **  **   → 2 stars + 2 spaces + 2 stars
Row 3: ******   → 3 stars + 0 spaces + 3 stars

Java Program

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

        // Top half
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++)
                System.out.print("*");

            for (int j = 1; j <= 2 * (n - i); j++)
                System.out.print(" ");

            for (int j = 1; j <= i; j++)
                System.out.print("*");

            System.out.println();
        }

        // Bottom half
        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= i; j++)
                System.out.print("*");

            for (int j = 1; j <= 2 * (n - i); j++)
                System.out.print(" ");

            for (int j = 1; j <= i; j++)
                System.out.print("*");

            System.out.println();
        }
    }
}
*      *
**    **
***  ***
********
********
***  ***
**    **
*      *

Possible Followup Questions with Answers

Q1: Can you print the same pattern with numbers instead of stars?

Yes, modify System.out.print("*") to System.out.print(i) or any logic for numbers.

Q2: How would you create a hollow butterfly pattern?

Add conditions inside star-printing loops to only print stars at the boundary (i.e., j == 1 || j == i), and spaces elsewhere.

Q3: How can we make the program dynamic for any n?

Take input from the user using Scanner and use that value in your loops.

Q4: What's the time complexity of this program?

The program runs in O(n²) time because each loop prints up to n characters across nested iterations.

Mallikarjuna Mallisetty, author
About the author

Mallikarjuna Mallisetty

General programming

Mallikarjuna shares practical programming tutorials and foundational concepts designed to help developers learn by building and experimenting.

View LinkedIn profile ↗