The butterfly star pattern is an advanced shape pattern in C programming that tests your control over bilateral symmetry, dual-wing rendering, and dynamic interior space calculations
Introduction
Mastering the butterfly pattern requires dividing the shape into two symmetrical halves, each constructed by printing left stars, middle spaces, and right stars across each row
Prerequisites: Comfort with nested
forloops, standard input/output (printf,scanf), and integer arithmetic. Expected Output:
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *
Deconstructing the Pattern Logic
For a half-height of n, the total height of the butterfly structure is 2n - 1 rows
Phase 1: Upper Wings (i = 1 to n)
Left Wing: Prints i asterisks
. Center Gap: Prints 2(n - i) spaces to form the shrinking inner gap
. Right Wing: Prints $i$ asterisks
.
Phase 2: Lower Wings (i = n - 1 down to 1)
Left Wing: Prints i asterisks
. Center Gap: Prints 2(n - i) spaces to expand the inner gap back outward
. Right Wing: Prints i asterisks
.
| Row Index (i) | Phase | Left Asterisks (i) | Center Spaces (2(n−i)) | Right Asterisks (i) |
| 1 | Upper | 1 | 8 | 1 |
| 2 | Upper | 2 | 6 | 2 |
| 3 | Upper | 3 | 4 | 3 |
| 4 | Upper | 4 | 2 | 4 |
| 5 (Center) | Upper | 5 | 0 | 5 |
| 4 (Row 6) | Lower | 4 | 2 | 4 |
| 3 (Row 7) | Lower | 3 | 4 | 3 |
| 2 (Row 8) | Lower | 2 | 6 | 2 |
| 1 (Row 9) | Lower | 1 | 8 | 1 |
Code Implementation
int main() {
int n, i, j;
printf("Enter the number of rows for upper half: ");
if (scanf("%d", &n) != 1 || n <= 0) {
printf("Invalid input. Please enter a positive integer.\n");
return 1;
}
// Phase 1: Upper Wings (Rows 1 to n)
for (i = 1; i <= n; i++) {
// Left Wing Asterisks
for (j = 1; j <= i; j++) {
printf("*");
}
// Center Gap Spaces
for (j = 1; j <= 2 * (n - i); j++) {
printf(" ");
}
// Right Wing Asterisks
for (j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
// Phase 2: Lower Wings (Rows n - 1 down to 1)
for (i = n - 1; i >= 1; i--) {
// Left Wing Asterisks
for (j = 1; j <= i; j++) {
printf("*");
}
// Center Gap Spaces
for (j = 1; j <= 2 * (n - i); j++) {
printf(" ");
}
// Right Wing Asterisks
for (j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Code Breakdown
Triple Inner Loops: Each row executes three consecutive inner loops: one for the left wing, one for the middle gap, and one for the right wing
. Decrementing Phase: Reversing the outer loop bounds in Phase 2 (
for (i = n - 1; i >= 1; i--)) reuses the exact same inner wing and space formulas while guaranteeing perfect vertical reflection. Solid Center Line: At i = n, the space formula 2(n - n) yields 0 spaces, creating a solid middle bar of 2n asterisks
.
Compiling and Execution
Compile and execute using GCC
Enter the number of rows for upper half: 5
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *
Common Mistakes & Troubleshooting
Duplicate Full Width Row: Starting Phase 2 at i = n instead of i = n - 1 prints two adjacent solid lines of length 2n, distorting the central axis
. Incorrect Space Multiplier: Forgetting the factor of 2 in
2 * (n - i)causes the wings to overlap prematurely. Missing Line Breaks: Omitting
printf("\n");after the right wing loop forces the entire butterfly structure onto a single line.
Complexity Analysis
Time Complexity: O(n^2) because two sequential nested loop blocks execute over 2n - 1 rows with inner loops proportional to n.
Space Complexity: O(1) auxiliary memory space, requiring only scalar integer variables
.
Conclusion
Coordinating bilateral loop logic with decrementing phase bounds provides a foundation for building complex, multi-segmented ASCII graphics
For all Pattern Programs list click here:
…till the next post, bye-bye & take care