C Program to Print Butterfly Pattern
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
How to Think About It
Algorithm
Code
#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; }
Dry Run
Let's trace the program with input n=4 to see how the butterfly pattern is printed.
Upper half, row 4
Print 4 stars, then 0 spaces, then 4 stars: ********
Upper half, row 3
Print 3 stars, then 2 spaces, then 3 stars: *** ***
Upper half, row 2
Print 2 stars, then 4 spaces, then 2 stars: ** **
Upper half, row 1
Print 1 star, then 6 spaces, then 1 star: * *
Lower half, row 1
Print 1 star, then 6 spaces, then 1 star: * *
Lower half, row 2
Print 2 stars, then 4 spaces, then 2 stars: ** **
Lower half, row 3
Print 3 stars, then 2 spaces, then 3 stars: *** ***
Lower half, row 4
Print 4 stars, then 0 spaces, then 4 stars: ********
| Row | Left Stars | Spaces | Right Stars | Printed Line |
|---|---|---|---|---|
| 4 | 4 | 0 | 4 | ******** |
| 3 | 3 | 2 | 3 | *** *** |
| 2 | 2 | 4 | 2 | ** ** |
| 1 | 1 | 6 | 1 | * * |
| 1 | 1 | 6 | 1 | * * |
| 2 | 2 | 4 | 2 | ** ** |
| 3 | 3 | 2 | 3 | *** *** |
| 4 | 4 | 0 | 4 | ******** |
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
#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; }
#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; }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Two separate loops | O(n^2) | O(1) | Clarity and simplicity |
| Single loop with conditional | O(n^2) | O(1) | Shorter code, moderate readability |
| Using functions | O(n^2) | O(1) | Readability and reusability |