Butterfly Pattern
C
Easy
6 views
Problem Description
* *
** **
*** ***
********
*** ***
** **
* * Logic: Upper half and lower half symmetric. Carefully calculate the pattern of stars and spaces.
Official Solution
#include <stdio.h>
int main() {
int n = 4; // Half of the butterfly (number of rows in upper half)
// Upper half
for (int i = 1; i <= n; i++) {
// Left stars
for (int j = 1; j <= i; j++)
printf("*");
// Spaces
for (int j = 1; j <= 2*(n-i); j++)
printf(" ");
// Right stars
for (int j = 1; j <= i; j++)
printf("*");
printf("n");
}
// Lower half
for (int i = n-1; i >= 1; i--) {
// Left stars
for (int j = 1; j <= i; j++)
printf("*");
// Spaces
for (int j = 1; j <= 2*(n-i); j++)
printf(" ");
// Right stars
for (int j = 1; j <= i; j++)
printf("*");
printf("n");
}
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!