A butterfly pattern is a symmetrical star pattern shaped like butterfly wings. The pattern consists of two mirrored triangles, with stars expanding outward and then contracting, creating the appearance of wings. This exercise helps learners practice nested loops and spacing in C.
Understanding The Problem
The program should take the number of rows (for half the butterfly) as input and print stars forming a butterfly pattern. Each row contains stars on both sides separated by spaces. Nested loops are used to control stars, spaces, and symmetry.
Steps to Solve the Problem:
- Take input for the number of rows for half the butterfly.
- Print the top half by expanding stars outward with decreasing spaces.
- Print the bottom half by contracting stars inward with increasing spaces.
- Ensure proper alignment and symmetry.
Solution: Using Nested Loops
Nested loops control the stars and spaces for the butterfly wings.
#include <stdio.h>
int main() {
int n;
printf("Enter number of rows for half the butterfly: ");
scanf("%d", &n);
// Top 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");
}
// Bottom half
for(int i = n; 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;
}
In this program, the top half expands stars outward with decreasing spaces, while the bottom half mirrors the top half. The formula 2*(n-i)
controls the number of spaces between the wings, ensuring symmetry.

FAQs
Q1: Can we use a character other than *
?
Yes, you can replace *
with any symbol to print a butterfly pattern using that character.
Q2: Can the pattern be adjusted for wider wings?
Yes, increasing the number of rows or adjusting the spacing formula can make the wings wider.
Q3: Can this pattern be printed with numbers instead of stars?
Yes, numbers or letters can replace stars to create numeric or alphabetic butterfly patterns.
Conclusion
This program demonstrates how to print a butterfly pattern using nested loops. By carefully controlling stars and spaces for each row, a symmetrical butterfly-shaped pattern is created. The same logic can be extended to hollow butterflies or patterns with different characters.
References & Additional Resources
- C Programming Tutorial – Covers loops, nested loops, and pattern printing in C.
- GeeksforGeeks – Pattern Programs in C – Examples of complex star patterns including butterfly patterns.
- TutorialsPoint – C Loops – Explanation of nested loops and conditional control for pattern printing in C.