C Program
#include <stdio.h> int main(){ int n=4; for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++) printf("*"); for(int j=1;j<=2*(n-i);j++) printf(" "); for(int j=1;j<=i;j++) printf("*"); printf("\n"); } for(int i=n;i>=1;i--){ for(int j=1;j<=i;j++) printf("*"); for(int j=1;j<=2*(n-i);j++) printf(" "); for(int j=1;j<=i;j++) printf("*"); printf("\n"); } }
C Output
Input: n = 4 Output: * * ** ** *** *** ******** ******** *** *** ** ** * *
C++ Program
#include <iostream> using namespace std; int main(){ int n=3; for(int i=1;i<=n;i++){ cout<<string(i,'*')<<string(2*(n-i),' ')<<string(i,'*')<<"\n"; } for(int i=n;i>=1;i--){ cout<<string(i,'*')<<string(2*(n-i),' ')<<string(i,'*')<<"\n"; } }
C++ Output
Input: n = 3 Output: * * ** ** ****** ****** ** ** * *
JAVA Program
public class Main{ public static void main(String[] args){ int n=5; for(int i=1;i<=n;i++) System.out.println("*".repeat(i)+" ".repeat(2*(n-i))+"*".repeat(i)); for(int i=n;i>=1;i--) System.out.println("*".repeat(i)+" ".repeat(2*(n-i))+"*".repeat(i)); } }
JAVA Output
Input: n = 5 Output: * * ** ** *** *** **** **** ********** ********** **** **** *** *** ** ** * *
Python Program
n=2 for i in range(1,n+1): print("*"*i+" "*(2*(n-i))+"*"*i) for i in range(n,0,-1): print("*"*i+" "*(2*(n-i))+"*"*i)
Python Output
Input: n = 2 Output: * * **** **** * *
Deep-Dive Explanation
Learning the Butterfly Pattern
The butterfly pattern is made with two halves — the top wings open out from one star to the fullest width, and the bottom wings close back in an even manner. The gaps between the left and the right wings give the impression that there are two different wings.
Step-by-Step Logic
In the top half:
Print i stars for the left wing.
Print 2*(n-i) spaces for the gap between wings.
Print i stars for the right wing.
In the bottom half:
Repeat the same pattern but loop i from n to 1.
Example with n = 4
Row 1 → * + spaces + *
Row 2 → ** + less spaces + **
Row 4 → **** + no spaces + ****
Then reverse this list for the bottom half.
Why This Pattern Matters
This habit builds strength in nested loops, symmetry generation, and space management — all critical to pattern problems and console graphics.
Real-Life Analogy
Drawing the outline of a butterfly on paper — the left and right wings are mirror images, and the gap in the middle is like the butterfly's body.
Practical Uses
Pretty console outputs for games
Text-based animations
Symmetrical design practice for UI text elements
Interview Insights
The butterfly pattern is a standard interview question used to gauge the ability to control loops and produce mirror patterns. Interviewers can vary it by turning it hollow, numeric, or character-based.
For SEO purposes:
The butterfly pattern code in C, C++, Java, and Python is an excellent exercise for beginners to learn nested loops and creating symmetrical patterns. The programming challenge allows one to practice space alignment, loop construction, and mirrored patterning. In preparation for exams, interviews, or competitive coding, the butterfly pattern enhances logical reasoning and problem-solving skills in programming.
Social Plugin