C Program to Print Butterfly Pattern

Introduction

A butterfly pattern is a symmetric star pattern that looks like the wings of a butterfly. It consists of two parts: an upper part where the stars form a wing-like structure, and a lower part which is the inverted version of the upper part. This exercise helps in understanding how to use loops and conditional logic to create symmetrical patterns.

Problem Statement

Create a C program that:

  • Accepts the number of rows for the butterfly pattern.
  • Prints a butterfly-shaped star pattern.

Example:

  • Input: rows = 5
  • Output:
    *       *
    **     **
    ***   ***
    **** ****
    *********
    **** ****
    ***   ***
    **     **
    *       *
    

Solution Steps

  1. Input the Number of Rows: The size determines the number of rows for each half of the butterfly.
  2. Use Nested Loops: The outer loops handle the rows, and the inner loops handle printing the stars and spaces.
  3. Display the Butterfly Pattern: Print stars and spaces to form the butterfly structure.

C Program

#include <stdio.h>

int main() {
    int i, j, rows;

    // Step 1: Accept the number of rows for the butterfly pattern
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    // Step 2: Print the upper part of the butterfly
    for (i = 1; i <= rows; i++) {
        // Print the left wing stars
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        // Print spaces between the wings
        for (j = 1; j <= 2 * (rows - i); j++) {
            printf(" ");
        }
        // Print the right wing stars
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }

    // Step 3: Print the lower part of the butterfly
    for (i = rows; i >= 1; i--) {
        // Print the left wing stars
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        // Print spaces between the wings
        for (j = 1; j <= 2 * (rows - i); j++) {
            printf(" ");
        }
        // Print the right wing stars
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Explanation

Step 1: Input Number of Rows

  • The program starts by taking the input from the user to define the number of rows for each half of the butterfly pattern.

Step 2: Print the Upper Part of the Butterfly

  • The outer loop controls the number of rows for the upper part.
  • The first inner loop prints stars for the left wing.
  • The second inner loop prints spaces between the left and right wings.
  • The third inner loop prints stars for the right wing.

Step 3: Print the Lower Part of the Butterfly

  • The second outer loop controls the rows for the lower part, which is the inverted version of the upper part.
  • The inner loops print stars and spaces in reverse order to mirror the upper part.

Output Example

For rows = 5, the output will be:

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

For rows = 6, the output will be:

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

Conclusion

This C program prints a butterfly pattern using nested loops to create symmetry between the upper and lower parts of the pattern. This exercise helps in practicing loop control, conditional printing, and creating symmetrical star patterns in C programming.

Comments