Butterfly Star pattern in C
Print the Butterfly pattern with N rows when given an integer value N.
Examples:
Input: 4
Output:
* *
** **
*** ***
*******
*** ***
** **
* *
Input: 6
Output:
* *
** **
*** ***
**** ****
***** *****
***********
***** *****
**** ****
*** ***
** **
* *
Have a look at the figure below to understand the butterfly pattern clearly. The figure shows 5 patterns with n values from 1 to 5.
This tutorial will explore two approaches to get a butterfly pattern using stars in C.
Method 1:
The three loops nested inside an outer loop can be used to solve the problem. The first inner loop prints the stars, the second inner loop prints the spaces, and the third inner loop prints the stars again. The outer loop runs for the rows.
Algorithm:
- To keep track of the number of gaps and asterisks in every row, keep two variables: gaps = 2 * x - 1 and asterisks = 0.
- Execute the outer loop by going from i = 1 to the number of rows (2 * x – 1).
- Reduce the number of gaps by two and the number of asterisks by one when we are in the upper portion of the butterfly.
- If we are in the butterfly's lower chamber, there will be a 2-asterisks decrease and a 1-gap increase.
- Execute an inner loop starting at j = 1 and ending at asterisks.
- Each time the inner loop iterates, print an asterisk.
- From j = 1 to gaps, execute an inner loop.
- Print a gap after the inner loop iterates each time.
- From j = 1 to asterisks, execute an inner loop.
- Each time the inner loop iterates, print an asterisk.
- Print the newline character ("\n") to advance to the following row.
- We will obtain the proper half-pyramid pattern after N iterations.
Program 1:
The application of the previously mentioned strategy is shown below:
#include <stdio.h>
int main()
{
int x = 6;
int gaps = 2 * x - 1;
int asterisks = 0;
for (int i = 1; i <= 2 * x - 1; i++) {
if (i <= x) {
gaps = gaps - 2;
asterisks++;
}
else {
gaps = gaps + 2;
asterisks--;
}
for (int j = 1; j <= asterisks; j++) {
printf("*");
}
for (int j = 1; j <= gaps; j++) {
printf(" ");
}
for (int j = 1; j <= asterisks; j++) {
if (j != x) {
printf("*");
}
}
printf("\n");
}
return 0;
}
Asterisks and gaps are used in the provided C code to create a butterfly pattern. The program initialises variables for the number of rows (x), gaps, and asterisks. Iterating through each row using a loop modifies the number of asterisks and gaps dynamically according to whether the row is in the upper or lower half of the pattern. The asterisks and gaps are printed by the nested loops inside the main loop, resulting in a butterfly-like pattern. The result displays a symmetric star arrangement that creates the appearance of butterfly wings. The program uses basic control structures and output functions from the C standard input/output library.
Output
Time Complexity: O(x^2), where x denotes the pattern's row count.
Auxiliary Space: O(1)
Program 2:
Let’s write down the C program to print a butterfly pattern differently.
#include <stdio.h>
#include <math.h>
void starss(int x)
{
while (x--)
{
printf("* ");
}
}
void gapss(int x)
{
while (x--)
{
printf(" ");
}
}
int main()
{
int length;
printf("Input the pattern's length: ");
scanf("%d", &length);
printf("Butterfly design with a length of %d: \n\n", length);
int gaps = 2 * (length - 1);
for (int i = 1; i <= length; i++)
{
starss(i);
gapss(gaps);
starss(i);
printf("\n");
gaps -= 2;
}
gaps = 2;
for (int i = length- 1; i > 0; i--)
{
starss(i);
gapss(gaps);
starss(i);
printf("\n");
gaps += 2;
}
return 0;
}
In this case, the variable length stores the pattern's length.
Printing stars and gaps is done with the starss and gapss methods. These two approaches count the times that stars or gaps are printed and display them on the console.
It determines how many gaps to print after the user enters their length.
The pattern's first half is printed by the first for loop, and its second half is printed by the second for loop. We can write it using any other loop as well.
This program will print the following output if you run it:
Output: