Butterfly Pattern

Butterfly Pattern

Easy C Pattern Printing 63 views
Explanation Complexity

Problem Statement

*      *
**    **
***  ***
********
***  ***
**    **
*      *     Logic: Upper half and lower half symmetric. Carefully calculate the pattern of stars and spaces.

Input Format

No input required.
Fixed number of rows and columns.

Output Format

Symmetric star pattern with upper and lower halves.

Constraints

• Fixed rows = 7

• Fixed columns = 8

• Pattern is vertically symmetric

Concept Explanation

The pattern is divided into upper half, middle row, and lower half.
Stars increase toward the center, then decrease symmetrically.

Step-by-Step Explanation

Upper Half (rows 0 to mid−1)

1.Let n = 4 (half height).

2.For row i from 0 to n-2:

3.Print i + 1 stars on the left.

4.Print 2*(n - i - 1) spaces in the middle.

5.Print i + 1 stars on the right.

6.Move to the next line.

Middle Row

1.Print 2*n stars continuously.

Lower Half (mirror of upper half)

1.For row i from n-2 down to 0:

2.Print i + 1 stars on the left.

3.Print 2*(n - i - 1) spaces in the middle.

4.Print i + 1 stars on the right.

5.Move to the next line.

Concept Explanation

The pattern is divided into upper half, middle row, and lower half.
Stars increase toward the center, then decrease symmetrically.

Step-by-Step Explanation

Upper Half (rows 0 to mid−1)

1.Let n = 4 (half height).

2.For row i from 0 to n-2:

3.Print i + 1 stars on the left.

4.Print 2*(n - i - 1) spaces in the middle.

5.Print i + 1 stars on the right.

6.Move to the next line.

Middle Row

1.Print 2*n stars continuously.

Lower Half (mirror of upper half)

1.For row i from n-2 down to 0:

2.Print i + 1 stars on the left.

3.Print 2*(n - i - 1) spaces in the middle.

4.Print i + 1 stars on the right.

5.Move to the next line.

Input / Output Format

Input Format
No input required.
Fixed number of rows and columns.
Output Format
Symmetric star pattern with upper and lower halves.
Constraints
• Fixed rows = 7

• Fixed columns = 8

• Pattern is vertically symmetric

Examples

Input:
Output:
* * ** ** *** *** ******** *** *** ** ** * *

Example Solution (Public)

C
#include <stdio.h>

int main() {
    int n = 4; // Half of the butterfly (number of rows in upper half)
    
    // Upper half
    for (int i = 1; i <= n; i++) {
        // Left stars
        for (int j = 1; j <= i; j++)
            printf("*");
        
        // Spaces
        for (int j = 1; j <= 2*(n-i); j++)
            printf(" ");
        
        // Right stars
        for (int j = 1; j <= i; j++)
            printf("*");
        
        printf("n");
    }
    
    // Lower half
    for (int i = n-1; i >= 1; i--) {
        // Left stars
        for (int j = 1; j <= i; j++)
            printf("*");
        
        // Spaces
        for (int j = 1; j <= 2*(n-i); j++)
            printf(" ");
        
        // Right stars
        for (int j = 1; j <= i; j++)
            printf("*");
        
        printf("n");
    }
    
    return 0;
}

Official Solution Code

#include <stdio.h>

int main() {
    int n = 4; // Half of the butterfly (number of rows in upper half)
    
    // Upper half
    for (int i = 1; i <= n; i++) {
        // Left stars
        for (int j = 1; j <= i; j++)
            printf("*");
        
        // Spaces
        for (int j = 1; j <= 2*(n-i); j++)
            printf(" ");
        
        // Right stars
        for (int j = 1; j <= i; j++)
            printf("*");
        
        printf("n");
    }
    
    // Lower half
    for (int i = n-1; i >= 1; i--) {
        // Left stars
        for (int j = 1; j <= i; j++)
            printf("*");
        
        // Spaces
        for (int j = 1; j <= 2*(n-i); j++)
            printf(" ");
        
        // Right stars
        for (int j = 1; j <= i; j++)
            printf("*");
        
        printf("n");
    }
    
    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.