C++ Program To Print Butterfly Pattern

 

C++ Program To Print Butterfly Pattern, butterfly pattern in c++, pattern ,pattern problem,

Program:

#include <iostream>
using namespace std;

int main()
{
    int n;
    cout<<"Enter number of rows: ";
    cin>>n;
    
    //loop for printing upper half
    for(int i=1;i<=n;i++)   //loop to terminate line
    {
        for(int j=1;j<=i;j++)   //loop to print *
        cout<<"* ";
        
        for(int j=1;j<=2*n-2*i;j++) //loop to print spaces
        cout<<"  ";
        
        for(int j=1;j<=i;j++)   //loop to print *
        cout<<"* ";
        
        cout<<endl;
    }

    //loop for printing lower half
    for(int i=n;i>=1;i--)
    {
        for(int j=1;j<=i;j++)
        cout<<"* ";
        
        for(int j=1;j<=2*n-2*i;j++)
        cout<<"  ";
        
        for(int j=1;j<=i;j++)
        cout<<"* ";
        
        cout<<endl;
    }
    return 0;
}

Output:

*        *
**      **
***    ***
****  ****
**********
**********
****  ****
***    ***
**      **
*        *
C++ Program To Print Butterfly Pattern C++ Program To Print Butterfly Pattern Reviewed by Beast Coder on February 14, 2021 Rating: 5

No comments:

Powered by Blogger.