So I am taking a C++ course during which I was asked to write a program to print the following butterfly pattern:
* *
** **
*** ***
********
********
*** ***
** **
* *
So, this is what I wrote:
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
for(int row=1; row<=n; row++){
for(int j=1; j<=2*n; j++){
if(j<=row||j>=(2*n)-row){
cout<<"*";
}
else{
cout<<" ";
}
}
cout<<endl;
}
for(int row=n; row>0; row--){
for(int j=1; j<=2*n; j++){
if(j<=row||j>=(2*n)-row){
cout<<"*";
}
else{
cout<<" ";
}
}
cout<<endl;
}
return 0;
}
but I couldn't get the desired result. What could be the reason?