Coders Packet

Hollow butterfly pattern printing in C++

By Ebtesam Ahmad Karimi

In this project, I will show you how to print hollow butterfly pattern using for loop in C++ programming language.

Problem Statement:

You have to print hollow butterfly pattern for a given input n. 

Input:

You have to enter an integer n.

Constraints:

3<=n<=50

Output:

Print the demanded hollow butterfly.

Example

Input:

5

Output:

Hollow butterfly pattern printing in C++ output

Following is the source code in C++ programming language for above problem statement:

#include
using namespace std;
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=i;j++){
            if(j==1 || j==i){
                cout<<"*";
            }else{
                cout<<" ";
            }
                
        }
        for(int j=1;j<=2*n-2*i;j++){
            cout<<" ";
        }
        for(int j=1;j<=i;j++){
            if(j==1 || j==i){
                cout<<"*";
            }else{
                cout<<" ";
            }
                
        }
        cout<<endl;
    }
    for(int i=n;i>=1;i--){
        for(int j=1;j<=i;j++){
            if(j==1 || j==i){
                cout<<"*";
            }else{
                cout<<" ";
            }
                
        }
        for(int j=1;j<=2*n-2*i;j++){
            cout<<" ";
        }
        for(int j=1;j<=i;j++){
            if(j==1 || j==i){
                cout<<"*";
            }else{
                cout<<" ";
            }
                
        }
        cout<<endl;
    }
    return 0;
}

 

Download project

Reviews Report

Submitted by Ebtesam Ahmad Karimi (ebtesam)

Download packets of source code on Coders Packet