New: 203 pattern programs with tested code in C, C++, Java and Python Explore patterns

Hollow Butterfly Star Pattern

Read summary with AI tools

The hollow butterfly star pattern keeps only the outline of the butterfly: two straight outer edges and two diagonals that meet in the middle, where the wings join.

Quick answer: Give every row 2n positions. Print a star on the outer columns 1 and 2n, and on the two diagonals j == i and j == 2n − i + 1. Run rows 1 to n, then the same rows from n back to 1.

This program is part of the Star Patterns collection.

Table of contents8 sections
  1. Output
  2. How the hollow butterfly star pattern works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Hollow Butterfly Star Pattern output

Output (n = 4):

*      *
**    **
* *  * *
*  **  *
*  **  *
* *  * *
**    **
*      *
Hollow Butterfly Star Pattern layout diagram, row by row (n = 4)
How each row of the hollow butterfly star pattern is built (n = 4). Blue cells are printed characters; grey dots are spaces.

How the hollow butterfly star pattern works

Each wing of the solid butterfly is a right triangle; its outline is the outer column plus the slanted edge. The left wing’s slanted edge is where j == i, the right wing’s is its mirror, j == 2n − i + 1. On row n the two edges touch in the middle. The lower wings are the same rows printed in reverse, and here the middle row is printed twice so the body is two stars tall.

  1. Run the outer loop for i from 1 to n, one row per pass (upper wings).
  2. Go through 2 × n positions in the row. Print stars where j = 1 or j = 2 × n or j = i or j = 2 × n − i + 1; print spaces everywhere else.
  3. Move to the next line.
  4. Run the outer loop for i from n down to 1, one row per pass (lower wings).
  5. Go through 2 × n positions in the row. Print stars where j = 1 or j = 2 × n or j = i or j = 2 × n − i + 1; print spaces everywhere else.
  6. Move to the next line.

Key takeaways

  • The outline is two outer columns plus two diagonals.
  • The right diagonal mirrors the left one: j == 2n − i + 1.
  • The two diagonals touch on the middle rows, forming the body.
  • The lower half repeats row n, matching the solid butterfly on this site.

Hollow Butterfly Star Pattern in C

#include <stdio.h>

int main(void) {
    int n = 4;

    // upper wings
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= 2 * n; j++) {
            if (j == 1 || j == 2 * n || j == i || j == 2 * n - i + 1)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }

    // lower wings
    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= 2 * n; j++) {
            if (j == 1 || j == 2 * n || j == i || j == 2 * n - i + 1)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

Hollow Butterfly Star Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 4;

    // upper wings
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= 2 * n; j++) {
            if (j == 1 || j == 2 * n || j == i || j == 2 * n - i + 1)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }

    // lower wings
    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= 2 * n; j++) {
            if (j == 1 || j == 2 * n || j == i || j == 2 * n - i + 1)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}

Hollow Butterfly Star Pattern in Java

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

        // upper wings
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= 2 * n; j++) {
                if (j == 1 || j == 2 * n || j == i || j == 2 * n - i + 1)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }

        // lower wings
        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= 2 * n; j++) {
                if (j == 1 || j == 2 * n || j == i || j == 2 * n - i + 1)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

Hollow Butterfly Star Pattern in Python

n = 4

# upper wings
for i in range(1, n + 1):
    for j in range(1, 2 * n + 1):
        if j == 1 or j == 2 * n or j == i or j == 2 * n - i + 1:
            print("*", end="")
        else:
            print(" ", end="")
    print()

# lower wings
for i in range(n, 0, -1):
    for j in range(1, 2 * n + 1):
        if j == 1 or j == 2 * n or j == i or j == 2 * n - i + 1:
            print("*", end="")
        else:
            print(" ", end="")
    print()

Time and space complexity

The program prints one character for each position in the pattern, so for n rows it runs in O(n²) time. It only needs a few loop variables, so the extra memory is O(1).

Frequently asked questions

Why is the middle row printed twice?

Both halves include row n, so the body is two rows tall, the same as in the solid butterfly star pattern. Start the second half at n − 1 to print it once.

How do I make the butterfly wider?

Raise n. The width is always 2n and the height 2n, so the shape grows evenly.

Can I fill the wings with a different character?

Yes. Add an else if for cells between the outer column and the diagonal and print a dot or a hash there.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Latest articles

Leave a comment

Your email address will not be published. Required fields are marked *

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