0
0
CProgramBeginner · 2 min read

C Program to Print Butterfly Pattern

A C program to print a butterfly pattern uses nested for loops to print stars and spaces symmetrically; for example, use two loops to print the left and right wings with spaces in the middle.
📋

Examples

Input4
Output**** **** *** *** ** ** * * * * ** ** *** *** **** ****
Input2
Output** ** * * * * ** **
Input1
Output* * * *
🧠

How to Think About It

To print a butterfly pattern, think of it as two mirrored triangles of stars separated by spaces. First, print the upper half by increasing stars on each side and decreasing spaces in the middle. Then print the lower half by reversing this pattern.
📐

Algorithm

1
Get the number of rows (n) from the user
2
For each row from 1 to n, print stars increasing on left and right sides with spaces decreasing in the middle
3
For each row from n down to 1, print stars increasing on left and right sides with spaces decreasing in the middle
4
Print a newline after each row
💻

Code

c
#include <stdio.h>

int main() {
    int n, i, j;
    scanf("%d", &n);

    for(i = n; i >= 1; i--) {
        for(j = 1; j <= i; j++) printf("*");
        for(j = 1; j <= 2*(n - i); j++) printf(" ");
        for(j = 1; j <= i; j++) printf("*");
        printf("\n");
    }

    for(i = 1; i <= n; i++) {
        for(j = 1; j <= i; j++) printf("*");
        for(j = 1; j <= 2*(n - i); j++) printf(" ");
        for(j = 1; j <= i; j++) printf("*");
        printf("\n");
    }

    return 0;
}
Output
**** **** *** *** ** ** * * * * ** ** *** *** **** ****
🔍

Dry Run

Let's trace the program with input n=4 to see how the butterfly pattern is printed.

1

Upper half, row 4

Print 4 stars, then 0 spaces, then 4 stars: ********

2

Upper half, row 3

Print 3 stars, then 2 spaces, then 3 stars: *** ***

3

Upper half, row 2

Print 2 stars, then 4 spaces, then 2 stars: ** **

4

Upper half, row 1

Print 1 star, then 6 spaces, then 1 star: * *

5

Lower half, row 1

Print 1 star, then 6 spaces, then 1 star: * *

6

Lower half, row 2

Print 2 stars, then 4 spaces, then 2 stars: ** **

7

Lower half, row 3

Print 3 stars, then 2 spaces, then 3 stars: *** ***

8

Lower half, row 4

Print 4 stars, then 0 spaces, then 4 stars: ********

RowLeft StarsSpacesRight StarsPrinted Line
4404********
3323*** ***
2242** **
1161* *
1161* *
2242** **
3323*** ***
4404********
💡

Why This Works

Step 1: Printing stars on left and right

The program prints stars on the left and right sides of each row using loops that run up to the current row number.

Step 2: Printing spaces in the middle

Spaces between the two star groups increase or decrease to create the butterfly shape, calculated as twice the difference between total rows and current row.

Step 3: Upper and lower halves

The upper half prints rows from n down to 1, and the lower half prints rows from 1 to n, mirroring the pattern.

🔄

Alternative Approaches

Using single loop with conditional
c
#include <stdio.h>

int main() {
    int n, i, j;
    scanf("%d", &n);

    for(i = 1; i <= 2*n; i++) {
        int stars = i <= n ? n - i + 1 : i - n;
        for(j = 1; j <= stars; j++) printf("*");
        for(j = 1; j <= 2*(n - stars); j++) printf(" ");
        for(j = 1; j <= stars; j++) printf("*");
        printf("\n");
    }

    return 0;
}
This method uses one loop for both halves, making code shorter but slightly harder to read.
Using functions to print parts
c
#include <stdio.h>

void printStars(int count) {
    for(int i = 0; i < count; i++) printf("*");
}

void printSpaces(int count) {
    for(int i = 0; i < count; i++) printf(" ");
}

int main() {
    int n, i;
    scanf("%d", &n);

    for(i = n; i >= 1; i--) {
        printStars(i);
        printSpaces(2*(n - i));
        printStars(i);
        printf("\n");
    }
    for(i = 1; i <= n; i++) {
        printStars(i);
        printSpaces(2*(n - i));
        printStars(i);
        printf("\n");
    }
    return 0;
}
Using functions improves readability and reusability but adds slight overhead.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses nested loops where the outer loop runs 2*n times and inner loops run up to n times, resulting in O(n^2) time.

Space Complexity

Only a few variables are used; no extra data structures, so space complexity is O(1).

Which Approach is Fastest?

Both main and alternative methods have similar time complexity; using a single loop reduces code length but not runtime.

ApproachTimeSpaceBest For
Two separate loopsO(n^2)O(1)Clarity and simplicity
Single loop with conditionalO(n^2)O(1)Shorter code, moderate readability
Using functionsO(n^2)O(1)Readability and reusability
💡
Use nested loops carefully to control stars and spaces for symmetrical patterns.
⚠️
Beginners often forget to print the correct number of spaces, breaking the butterfly shape symmetry.