Print Butterfly Pattern

Print Butterfly Pattern

Hard C++ Pattern Printing 67 views
Explanation Complexity

Problem Statement

Print butterfly shape using stars.
Real Life: Complex symmetric pattern.

Input Format

An integer n (number of rows for one half).

Output Format

Butterfly pattern printed using *.

Example

4
*      *
**    **
***  ***
********
********
***  ***
**    **
*      *

Constraints

• n ≥ 1

• Use loops only

• Pattern must be symmetric

Concept Explanation

Butterfly pattern is a symmetric star pattern.
It has two parts:

• Upper half

• Lower half
Both parts are mirror images of each other.

Step-by-Step Explanation

Upper Half (rows 1 to n)

1.For each row i:

2.Print i stars on the left.

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

4.Print i stars on the right.

5.Move to next line.

Lower Half (rows n to 1)
6. For each row i:
7. Print i stars on the left.
8. Print 2*(n - i) spaces in the middle.
9. Print i stars on the right.
10. Move to next line.

Concept Explanation

Butterfly pattern is a symmetric star pattern.
It has two parts:

• Upper half

• Lower half
Both parts are mirror images of each other.

Step-by-Step Explanation

Upper Half (rows 1 to n)

1.For each row i:

2.Print i stars on the left.

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

4.Print i stars on the right.

5.Move to next line.

Lower Half (rows n to 1)
6. For each row i:
7. Print i stars on the left.
8. Print 2*(n - i) spaces in the middle.
9. Print i stars on the right.
10. Move to next line.

Input / Output Format

Input Format
An integer n (number of rows for one half).
Output Format
Butterfly pattern printed using *.
Constraints
• n ≥ 1

• Use loops only

• Pattern must be symmetric

Examples

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

Example Solution (Public)

C++
void pattern_q11_butterfly() {
    int n = 5;
    
    // Upper half
    for(int i = 1; i <= n; i++) {
        // Left stars
        for(int j = 1; j <= i; j++) {
            cout << "*";
        }
        // Middle spaces
        for(int j = 1; j <= 2*(n-i); j++) {
            cout << " ";
        }
        // Right stars
        for(int j = 1; j <= i; j++) {
            cout << "*";
        }
        cout << endl;
    }
    
    // Lower half
    for(int i = n; i >= 1; i--) {
        for(int j = 1; j <= i; j++) {
            cout << "*";
        }
        for(int j = 1; j <= 2*(n-i); j++) {
            cout << " ";
        }
        for(int j = 1; j <= i; j++) {
            cout << "*";
        }
        cout << endl;
    }
}

Official Solution Code

void pattern_q11_butterfly() {
    int n = 5;
    
    // Upper half
    for(int i = 1; i <= n; i++) {
        // Left stars
        for(int j = 1; j <= i; j++) {
            cout << "*";
        }
        // Middle spaces
        for(int j = 1; j <= 2*(n-i); j++) {
            cout << " ";
        }
        // Right stars
        for(int j = 1; j <= i; j++) {
            cout << "*";
        }
        cout << endl;
    }
    
    // Lower half
    for(int i = n; i >= 1; i--) {
        for(int j = 1; j <= i; j++) {
            cout << "*";
        }
        for(int j = 1; j <= 2*(n-i); j++) {
            cout << " ";
        }
        for(int j = 1; j <= i; j++) {
            cout << "*";
        }
        cout << endl;
    }
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.