Print a Butterfly Alphabet Pattern in C
To print a butterfly alphabet pattern in C, we use nested loops to create the upper and lower halves of the pattern. The pattern consists of letters arranged symmetrically to form a butterfly shape, where the left and right wings mirror each other.
Example 1: Basic Butterfly Alphabet Pattern Using Characters
In this example, we will create a butterfly pattern using letters starting from ‘A’. The pattern will be symmetric, where the left and right sides mirror each other.
main.c
</>
Copy
#include <stdio.h>
int main() {
int n = 5;
// Upper half of the butterfly
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%c ", 'A' + j - 1);
}
for (int j = 1; j <= 2 * (n - i); j++) {
printf(" ");
}
for (int j = i; j >= 1; j--) {
printf("%c ", 'A' + j - 1);
}
printf("\n");
}
// Lower half of the butterfly
for (int i = n-1; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("%c ", 'A' + j - 1);
}
for (int j = 1; j <= 2 * (n - i); j++) {
printf(" ");
}
for (int j = i; j >= 1; j--) {
printf("%c ", 'A' + j - 1);
}
printf("\n");
}
return 0;
}
Explanation:
- We declare an integer
n = 5
to define the pattern size. - The first nested loop prints the upper half of the butterfly:
- The first
for
loop prints letters from ‘A’ up to the current row number. Refer: C For Loop. - The second
for
loop prints spaces to create the gap in the middle. - The third
for
loop prints letters in reverse order to complete the mirror effect.
- The first
- The second nested loop prints the lower half, using the same logic but in reverse order.
Output:
A A
A B B A
A B C C B A
A B C D D C B A
A B C D E E D C B A
A B C D D C B A
A B C C B A
A B B A
A A
Example 2: Butterfly Alphabet Pattern with Repeating Letters
In this example, we modify the butterfly pattern so that each row contains repeating letters instead of sequential characters.
main.c
</>
Copy
#include <stdio.h>
int main() {
int n = 5;
// Upper half
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%c ", 'A' + i - 1);
}
for (int j = 1; j <= 2 * (n - i); j++) {
printf(" ");
}
for (int j = 1; j <= i; j++) {
printf("%c ", 'A' + i - 1);
}
printf("\n");
}
// Lower half
for (int i = n-1; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("%c ", 'A' + i - 1);
}
for (int j = 1; j <= 2 * (n - i); j++) {
printf(" ");
}
for (int j = 1; j <= i; j++) {
printf("%c ", 'A' + i - 1);
}
printf("\n");
}
return 0;
}
Explanation:
- We set
n = 5
to determine the number of rows. - The first loop prints the upper half:
- Each row prints the same letter corresponding to its row number.
- Spaces are added in the middle to maintain the butterfly shape.
- The second loop prints the lower half in reverse order.
Output:
A A
B B B B
C C C C C C
D D D D D D D D
E E E E E E E E E E
D D D D D D D D
C C C C C C
B B B B
A A
Conclusion
In this tutorial, we explored how to print butterfly alphabet patterns in C. We covered:
- Basic Butterfly Alphabet Pattern: A symmetrical pattern using sequential characters.
- Repeating Letters Butterfly Pattern: Each row consists of the same letter for a different effect.