Advanced Patterns Problems | C++

Part-6: Advanced pattern problems

Advanced pattern problems mean such patterns that are made with a combination of simple patterns. These can be of any type depending on our creativity. Here we will discuss a few patterns.

Topics Covered:

  1. Butterfly pattern using stars
  2. Hollow Butterfly pattern using stars
  3. Zig-zag pattern using stars
  4. hollow full pyramid pattern using stars

1. Butterfly pattern using stars

*        *
**      **
***    ***
****  ****
**********
**********
****  ****
***    ***
**      **
*        *

When we observe this pattern, this is the collection of four half pyramids that we made in previous parts but their alignment is different. Firstly a half pyramid then in right to it there is a pyramid with 180-degree rotation and below is the water image of the upper part. So let’s try this concept with code

CODE:

#include<iostream>
using namespace std;
int main()
{
	int h;
	cout<<"Enter the height "<<endl;
	cin>>h;
	
	for(int i=1; i<=h; i++)
	{
		int space=2*h-2*i;
		for(int j=1; j<=i; j++)
		{
			cout<<"*";
		}
		for(int j=1; j<=space; j++)
		{
			cout<<" ";
		}
		for(int j=1; j<=i; j++)
		{
			cout<<"*";
		}
		cout<<endl;
	}
	
	for(int i=h; i>=1; i--)
	{
		int space=2*h-2*i;
		for(int j=1; j<=i; j++)
		{
			cout<<"*";
		}
		for(int j=1; j<=space; j++)
		{
			cout<<" ";
		}
		for(int j=1; j<=i; j++)
		{
			cout<<"*";
		}
		cout<<endl;
	}
	return 0;
}

OUTPUT:

2. Hollow Butterfly pattern using stars

*        *
**      **
* *    * *
*  *  *  *
*   **   *
*   **   *
*  *  *  *
* *    * *
**      **
*        *

This pattern is also the collection of four hollow half pyramids but the difference is, none of these have their base side. So, we have to put an if-else condition in above program to execute the same. Let’s try this with code

CODE:

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

OUTPUT:

3. Zig-zag pattern using stars

    *       *       *       *
  *   *   *   *   *   *   *   *
*       *       *       *       *

CODE:

#include<iostream>
using namespace std;
int main()
{
	int l;
	cout<<"Enter the length :"<<endl;
	cin>>l;
	
	for(int i=1; i<=3;i++)
	{
		for(int j=1;j<=l;j++)
		{
			if(((i+j)%4==0) || ((i==2) && (j%4==0)))
			cout<<"* ";
			else cout<<"  ";
		}
		cout<<endl;
	}
	return 0;
}

OUTPUT:

4. Hollow full pyramid pattern using stars

     *
    * *
   *   *
  *     *
 *********

CODE:

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

OUTPUT:

In this part, we have discussed some of the advanced pattern problems. This is the last part of this pattern problem series. Now, try to make some more different patterns with the help of this series.

We will be happy to hear your thoughts

Leave a reply

Edusera
Logo
Open chat
1
Scan the code
Hello!👋
Can we help you?