# 2 different C programs to draw a butterfly pattern ## How to draw a butterfly pattern in C:

In this post, we will learn how to draw a butterfly pattern in C. We will learn how to derive the algorithm and how to write a program from the algorithm. The program will take the height of the butterfly pattern as an input from the user and it will print that pattern using star or any other character. We will also change the program to take the character as well as input from the user.

### Butterfly pattern:

A butterfly pattern of height 6 looks as like below:

``````*                     *
* *                 * *
* * *             * * *
* * * *         * * * *
* * * * *     * * * * *
* * * * * * * * * * * *
* * * * *     * * * * *
* * * *         * * * *
* * *             * * *
* *                 * *
*                     * ``````

Similarly, if the height is 4, it will be:

``````*             *
* *         * *
* * *     * * *
* * * * * * * *
* * *     * * *
* *         * *
*             * ``````

We are using star and spaces to draw this pattern. To write a program that prints this pattern, let me change the spaces to different characters:

``````* \$ \$ \$ \$ \$ \$ *
* * \$ \$ \$ \$ * *
* * * \$ \$ * * *
* * * * * * * *
* * * # # * * *
* * # # # # * *
* # # # # # # *``````
• The program will use two loops to print the top and the bottom part of this pattern.
• For the 1st row, there are 6 spaces with 1 star on both sides of the spaces.
• For the 2st row, there are 4 spaces with 2 stars on both sides of the spaces.
• For the 3rd row, there are 2 spaces with 3 stars on both sides of the spaces.
• The number of spaces on first line is 2 * (height - 1). On each line, this value is decreased by 2. Once it reaches 0, it increases by 2 on each line. Similarly, the stars keep increasing from the first line and it starts to decrease.

The program will use the below algorithm:

• Take the height of the butterfly pattern as input from the user.
• Create one variable, let’s say it is s, to hold the number of spaces to print. Initialize it as 2 * (height - 1).
• Run one loop from i = 1 to i = height, where i is the variable used in the loop. On each iteration,

• Print stars for i number of times.
• Print spaces for s number of times.
• Print stars for i number of times.
• Decrement the value of s by 2.
• Increment the value of i by 1.
• Print a new line.
• Once the first loop will complete, it will print half of the pattern. We have to run one more loop to print the second half. Run this loop from i = height - 1 to i = 1. Before the loop starts, assign 2 to s. On each iteration:

• Print the stars for i number of times.
• Print spaces for s number of times.
• Print stars for i number of times.
• Increment the value of s by 2.
• Decrement the value of i by 1.
• Print a new line.

### Example 1: C program to print a butterfly pattern:

Let’s write down the C program to print a butterfly pattern by using the above algorithm:

``````#include <stdio.h>
#include <math.h>

void printStar(int t)
{
while (t--)
{
printf("* ");
}
}

void printSpaces(int t)
{
while (t--)
{
printf("  ");
}
}

int main()
{
int height;

printf("Enter the height of the pattern: ");
scanf("%d", &height);
printf("Butterfly pattern of height %d: \n\n", height);

int spaces = 2 * (height - 1);

for (int i = 1; i <= height; i++)
{
printStar(i);
printSpaces(spaces);
printStar(i);
printf("\n");
spaces -= 2;
}

spaces = 2;
for (int i = height - 1; i > 0; i--)
{
printStar(i);
printSpaces(spaces);
printStar(i);
printf("\n");
spaces += 2;
}
return 0;
}``````

Here,

• height is the variable to hold the height of the pattern.
• The printStar method is used to print stars and the printSpaces method is used to print spaces. Both of these methods take the number of times to print stars or spaces and print them on the console.
• Once the height is entered by the user, it calculates the number of spaces to print.
• The first for loop prints the first half of the pattern and the second for loop prints the second half of the pattern. We can also use any other loops to write it.

If you run this program, it will print output as below:

``````Enter the height of the pattern: 4
Butterfly pattern of height 4:

*             *
* *         * *
* * *     * * *
* * * * * * * *
* * *     * * *
* *         * *
*             *

Enter the height of the pattern: 5
Butterfly pattern of height 5:

*                 *
* *             * *
* * *         * * *
* * * *     * * * *
* * * * * * * * * *
* * * *     * * * *
* * *         * * *
* *             * *
*                 * ``````

### Example 2: C program to print a butterfly pattern using any character:

Let’s modify the above example to use any other character to print the pattern.

``````#include <stdio.h>
#include <math.h>

void printChar(int t, char c)
{
while (t--)
{
printf("%c ", c);
}
}

void printSpaces(int t)
{
while (t--)
{
printf("  ");
}
}

int main()
{
int height;
char c;

printf("Enter the height of the pattern: ");
scanf("%d", &height);

getchar();
printf("Enter the character to use in the pattern: ");
scanf("%c", &c);

printf("Butterfly pattern of height %d: \n\n", height);

int spaces = 2 * (height - 1);

for (int i = 1; i <= height; i++)
{
printChar(i, c);
printSpaces(spaces);
printChar(i, c);
printf("\n");
spaces -= 2;
}

spaces = 2;
for (int i = height - 1; i > 0; i--)
{
printChar(i, c);
printSpaces(spaces);
printChar(i, c);
printf("\n");
spaces += 2;
}
return 0;
}``````

This is similar to the first example. It reads the character as input from the user and stores it in the character variable c. The method to print star is changed to take the character as well as its parameter. If you run this program, it will print output as below:

``````Enter the height of the pattern: 5
Enter the character to use in the pattern: #
Butterfly pattern of height 5:

#                 #
# #             # #
# # #         # # #
# # # #     # # # #
# # # # # # # # # #
# # # #     # # # #
# # #         # # #
# #             # #
#                 #

Enter the height of the pattern: 6
Enter the character to use in the pattern: ^
Butterfly pattern of height 6:

^                     ^
^ ^                 ^ ^
^ ^ ^             ^ ^ ^
^ ^ ^ ^         ^ ^ ^ ^
^ ^ ^ ^ ^     ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
^ ^ ^ ^ ^     ^ ^ ^ ^ ^
^ ^ ^ ^         ^ ^ ^ ^
^ ^ ^             ^ ^ ^
^ ^                 ^ ^
^                     ^ ``````