Pages

Monday, March 31, 2025

Butterfly Pattern in C++ (Learn this MISTAKE to BOOST up!!)

 #include <iostream>

using namespace std;
// quote- break down bigger problems into smaller problems
// Butterfly Pattern -


/*
pseudocode
1. build first pyramid
then second pyramid
*/
int main() {
    int n = 4;

    // upper half
    for(int i=1; i <= n; i++){
        //stars
        for(int j = 1; j <= i; j++){
            cout << "*";
        }   //spaces
            for(int j =1; j <= 2*(n-i); j++){
                cout << " ";
            }   //stars
            for(int j = 1; j <= i; j++){
                cout << "*";
            }
            cout << endl;
    }

    // lower half , Mylearnings- just change outer loop to make it opposite pattern
    for(int i = n; i >= 1; i--){ // outer loop(n to 1) so
// int i = n; i >= 1; i--
        // stars
        for(int j = 1; j <= i; j++){
            cout << "*";
        }
        // spaces
        for(int j = 1; j <= 2*(n-i); j++ ){
            cout << " ";
        }
        // stars
        for(int j = 1; j <= i; j++){
            cout << "*";
        }
        cout << endl;
    }
    return 0;

}

OUTPUT-
* * ** ** *** *** ******** ******** *** *** ** ** * *

No comments:

Post a Comment

Stack using Linked List – Partial (University Exam Topic)

 #include <iostream> using namespace std; struct Node {     int data;     Node* next; }; class Stack {     Node* top; public:     Stac...