20 complex patterns in C++

Here in this Page you found all Complex Patterns Programs List in C++


Before starting to Create Patterns Programs in C++ Make sure you read Create Patterns in C++

Complex Patterns Programs List In C++

1. create a solid square in c++

#include<iostream>
using namespace std;  
int main()
 {
    int i,j,n;
    cout<<"Enter Number: ";
    cin>>n;

    for(i=1; i<=n; i++)
     {
       int j=1;
       while(j<=n*2)
	{
	     cout<<"*";
	     j++;
       }
       cout<<endl;
     }

return 0;
  }

OUTPUT

Enter Number of rows: 5

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

2. create a pyramid in c++

#include<iostream>
using namespace std; 

 int main()
 {
   int row;
   cout<<"Enter number of rows: ";
   cin>>row;

    for(int i=1; i<=row; i++)
     {
	int j=1;
	while(j<=row-i)
	 {
	   cout<<" ";
	   j++;
	 }
	for(int k=0; k<=(i*2)-2; k++)
	    cout<<"*";
        cout<<endl;
     }
return 0;
 }

OUTPUT

Enter number of rows: 5

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

Here is another one,

#include<iostream>
using namespace std; 
int main()
  {
    int row,count=1;
    cout<<"Enter Number of rows: ";
    cin>>row;

     for(int i=1; i<=row; i++)
      {
	for(int j=1;j<=row-i;j++)
	    cout<<" ";

	for(int k=i; k>=1; k--)
	 {
	    if(k==1)
	       cout<<count++;
	    else
	       cout<<"*";
	 }

	for(int l=2; l<=i; l++)
	   cout<<"*";

	cout<<endl;
     }
return 0;
 }

OUTPUT

Enter Number of rows: 5

    1       
   *2* 
  **3**  
 ***4*** 
****5****

3. creating a reverse pyramid in c++

#include<iostream>
using namespace std; 
int main()
  {
    int i,j,n;clrscr();
    cout<<"Enter number of row: "; 
    cin>>n;

    cout<<endl;

     for(i=1; i<=n; i++)
      {
	for(j=0; j<i; j++)
	   cout<<" ";
	for(int k=0; k<=n*2-i*2; k++)
	   cout<<"*";
	cout<<endl;
      }
return 0;
 }

OUTPUT

Enter number of row: 5

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

Here is another one,

#include<iostream> 
using namespace std;
 
int main()
  {
   int row,i,j,k;
   cout<<"Enter Number of rows: "; 
   cin>>row;

   cout<<endl;

    i=1;
    while(i<=row)
     {
       for(j=1;j<=i;j++)
	   cout<<" ";

       for(k=1; k<=row-i; k++)
	   cout<<"*"<<" ";

       cout<<endl;
     i++;
    }
return0;
 }

OUTPUT

Enter Number of rows: 6

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

4 .create a  straight pyramid and reverse pyramid

#include<iostream> 
using namespace std; 
 int main()
  {
   int row,i,j,k;
   cout<<"Enter the number: "; 
   cin>>row;

   cout<<endl;
//upper part
    for(i=1;i<=row;i++)
     {
       for(j=1;j<=row-i;j++)
	   cout<<" ";

       for(k=1; k<=i*2-1; k++)
	   cout<<"*";

       cout<<endl;
     }

//lower part
   for(i=1;i<=row;i++)
     {
       for(j=1;j<=i;j++)
	   cout<<" ";

       for(k=1; k<=(row-i)*2-1; k++)
	   cout<<"*";

       cout<<endl;
     }

return 0;
 }

OUTPUT

Enter Number of rows: 5
          
    *  
   *** 
  *****  
 ******* 
********* 
 *******
  *****    
   ***   
    *

5. create triangle with other reverse triangle

#include<iostream> 
using namespace std; 
int main()
  {
   int row,i,j,k;
   cout<<"Enter the number: "; 
   cin>>row;

   cout<<endl;

//upper part
     for(i=1;i<=row;i++)
       {
	 for(j=1;j<=row-i;j++)
	   cout<<" ";

	 for(k=1; k<=i*2-1; k++)
	  {
	    if(k==1 || k==i*2-1)
	       cout<<"*";
	    else
	      cout<<" ";
	  }

	cout<<endl;
      }

//lower part
   for(i=1;i<=row;i++)
     {
       for(j=1;j<=i;j++)
	   cout<<" ";

       for(k=1; k<=(row-i)*2-1; k++)
	 {
	    if(k==1 || k==(row-i)*2-1)
	      cout<<"*";
	    else
	      cout<<" ";
	 }

       cout<<endl;
     }

return 0;
 }

OUTPUT

Enter the number: 5
              
    *  
   * *  
  *   * 
 *     * 
*       * 
 *     *
  *   *
   * *
    *

6. create a isoscles triangle in c++

#include<iostream> 
using namespace std; 
int main()
  {
    int i,j,k,row;
    cout<<"Enter Number of rows: "; 
    cin>>row;

     for(i=1;i<=row;i++)
      {
        for(j=1;j<=row-i;j++)
	   cout<<" ";
        for(k=1;k<=2*i-1;k++)
	 {
	   if(i<row)
	     {
	       if((k==1)||(k==2*i-1))
	           cout<<"*";
	       else
		  cout<<" ";
	     }
	   if(i==row)
	      cout<<"*";
         }
        cout<<endl;
      }

return 0;
 }

OUTPUT

Enter Number of rows: 5

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

7. create a equilateral triangle in c++

#include<iostream> 
using namespace std; 
int main()
 {
   int row;
   cout<<"Enter number of rows: "; 
   cin>>row;

   cout<<endl;

    for(int i=1; i<=row; i++)
     {
	int j=1;
	while(j<=row-i)
	 {
	   cout<<"  ";
	   j++;
	 }
	for(int k=0; k<=(i*2)-2; k++)
	  {
	   if(k==0 || i==row || k==(i*2)-2)
	      cout<<" *";
	   else
	      cout<<"  ";
	  }
       cout<<endl;
     }
 return 0;
 }

OUTPUT

Enter number of rows: 5

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

Here another one,

#include<iostream>
using namespace std; 
int main()
 {
   int row;
   cout<<"Enter number of rows: "; 
   cin>>row;

   cout<<endl;

    int i=1;
    while(i<=row)
     {
	for(int j=1; j<=row*2-i*2;j++)
	    cout<<" ";

	for(int k=1; k<=((2*i)-1)*2; k++)
	  {
	    if(i==1)
	     {
	          if(k==1)  
                     cout<<"/\\";
	     }						     
	    else if(k==1)
		  cout<<"/";
	    else if(k==((2*i)-1)*2)
		  cout<<"\\";
	    else if(i==row)
		  cout<<"_";
	    else
		  cout<<" ";
	  }
       cout<<endl;
       i++;
     }
return 0;
 }

OUTPUT

Enter number of rows: 5

            /\
          /    \
        /        \
      /            \
    /________________\

8. create a right-angle in c++

#include<iostream> 
using namespace std; 
int main()
  {
   int row,i,j;
   cout<<"Enter the number: "; 
   cin>>row;

   i=1;
   while(i<=row)
    {
      for(j=1;j<=i;j++)
       {
	 if(i==1 || j==1 || i==j || i==row)
	   cout<<"*";
	 else
	   cout<<" ";
       }
      cout<<endl;
      i++;
    }

return 0;
 }

OUTPUT

Enter the number: 5

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

9. create a obtuse triangle in c++

#include<iostream>
using namespace std; 
 int main()
  {
    int row,i,j,s;
    cout<<"Enter size: "; 
    cin>>row;

    if(row%2==0)
       s = 0;
    else
      s  = 1;

     i=1;
     while(i<=row)
      {
	for(int k = row/2; k<=i; k++)
	    cout<<" ";

	for(j=row; j<=i*2-s; j++)  // column
	 {
	   if(j==row||j==i*2-s||i==row)
	     cout<<"*";
	   else
	     cout<<" ";
	 }
	cout<<endl;
	i++;
      }
return 0;
 }

OUTPUT

Enter size: 9

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

10. create a rectangle in c++

#include<iostream> 
using namespace std; 
int main()
  {
   int i,j,row;
   cout<<"Enter Number of rows: "; 
   cin>>row;

       i=1;
       while(i<=row)
	{
	  for(j=1; j<=row; j++)
	   {
	     if(i==1)
		cout<<"*";

	     else if((j==1)||(j==row)|| (i==row))
	      cout<<"*";
	     else
	      cout<<" ";

	  } i++;
	 cout<<endl;
     }

return 0;
  }

OUTPUT

Enter Number of rows: 5

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

11. create a square in c++

#include<iostream> 
using namespace std;
 
int main()
  {
   int i,j,row;
   cout<<"Enter Number of rows: "; cin>>row;

    for(i=1; i<=row; i++)
     {
       int j=1;
       while(j<=row*2)
	{
	  if((i==1)||(i==row))
	      cout<<"*";

	  else if((j==1)||(j==row*2))
	      cout<<"*";
	  else
	      cout<<" ";
	 j++;
       }

       cout<<endl;
     }
return 0;
  }

OUTPUT

Enter Number of rows: 5

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

12. create hexagon in c++

#include<iostream> 
using namespace std; 
int main()
  {
   int row,i,j,k;
   cout<<"Enter the number: "; cin>>row;

   cout<<endl;

   int upper = row;

//upper part
     for(i=1;i<upper;i++)
       {
	 for(j=1;j<=upper-i;j++)
	   cout<<" ";

	 for(k=1; k<=i*2-1; k++)
	  {
	    if(i<=upper/2)
	       cout<<" ";
	    else if(i==(upper/2)+1)
	       cout<<"*";
	    else if(k==1|| k==i*2-1)
	       cout<<"*";
	    else if(i==upper-1)
	       cout<<" ";
	    else cout<<" ";
	  }
	 cout<<endl;
      }

//lower part
   int lower=row; int col;

    if(lower%2==0)   //check given number even or odd
      col = (lower/2)-1;
   else
      col = (lower/2);

   for(i=1;i<=lower;i++)
     {
       for(j=1;j<=i+1;j++)
	   cout<<" ";

       for(k=1; k<=(lower-i)*2-3; k++) { if(i>=col)
	       cout<<" ";
	    else if(i==col-1)
	       cout<<"*";
	    else if(k==1|| k==(lower-i)*2-3)
	       cout<<"*";
	    else if(i==lower-1)
	       cout<<" ";
	    else cout<<" ";
       }
       cout<<endl;
     }
return 0;
 }

OUTPUT

Enter the number: 8

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

13. create more than one pyramids in c++

#include<iostream>
using namespace std; 
int main()
  {
    int i,j,k,l,row,column;
    cout<<"Enter Height of Pyramid: "; cin>>row;
    cout<<"Enter Number of Pyramid: "; cin>>column;

      for(i=0;i<=row;i++)
       {
	 int t=0;
	 while(t!=column)
	  {
	     for(j=0;j<row-i;j++)
		cout<<" ";

	     for(k=1;k<=i*2-1;k++)
		cout<<"*";

	     for(l=1;l<=row-i;l++)
		cout<<" ";

	     t++;
	  }
	 cout<<endl;
       }

return 0;
 }

OUTPUT

Enter Height of Pyramid: 5
Enter Number of Pyramid: 6  
                                                                                
    *        *        *        *        *        * 
   ***      ***      ***      ***      ***      *** 
  *****    *****    *****    *****    *****    *****
 *******  *******  *******  *******  *******  *******
******************************************************

Here is another one,

#include<iostream> 
using namespace std; 
 int main()
  {
    int i,j,k,l,row,column;
    cout<<"Enter Height of Pyramid: "; cin>>row;
    cout<<"Enter Number of Pyramid: "; cin>>column;

      for(i=0;i<=row;i++)
       {
	 int t=0;
	 while(t!=column)
	  {
	     for(j=0;j<row-i;j++)
		cout<<"  ";

	     for(k=1;k<=i*2-1;k++)
	      {
		if(k==1||k==i*2-1)
		    cout<<"* ";
		else if(i==row)
		    cout<<"* ";
		else
		    cout<<"  ";
	      }

	     for(l=1;l<=row-i;l++)
		cout<<"  ";
	     t++;
	  }
	 cout<<endl;
       }
return 0;
 }

OUTPUT

Enter Height of Pyramid: 4
Enter Number of Pyramid: 4
                                                                                
      *             *             *             *
    *   *         *   *         *   *         *   *
  *       *     *       *     *       *     *       *
* * * * * * * * * * * * * * * * * * * * * * * * * * * *

14. create reverse to straight pyramid in c++

#include<iostream> 
using namespace std;
 
int main()
  {
    int row,i,k,l,j;
    cout<<"Enter row of NumberX2: "; cin>>row;

    cout<<endl;

 //upper part
     for(i=1; i<=row;i++)
      {
	for(j=1; j<i;j++)
	  cout<<" ";

	for(k=1; k<=row-i+1; k++)
	  cout<<"*"; for(l=row-i;l>=1;l--)
	  cout<<"*";

	cout<<endl;
      }


 //lower part
    for(i=1; i<=row; i++)
     {
	for(j=1; j<=row-i; j++)
	    cout<<" ";

	for(k=1; k<=i; k++)
	    cout<<"*"; for(l=i-1;l>=1;l--)
	    cout<<"*";

	cout<<endl;
     }

return 0;
 }

OUTPUT

Enter row of NumberX2: 5
                       
********* 
 ******* 
  *****
   ***
    * 
    * 
   *** 
  ***** 
 ******* 
*********

15. create right-angle from left and right side

#include<iostream>
using namespace std; 
int main()
  {
    int n,i,j,k,l;
    cout<<"Enter row of length: "; cin>>n;

    cout<<endl;

     for(i=1;i<=n;i++)
      {
	 for(j=1;j<=i;j++)
	    cout<<"*";

	 for(k=i;k<=n+n-i;k++)
	    cout<<" "; for(l=i;l>=1;l--)
	    cout<<"*";

	 cout<<endl;
      }

return 0;
 }

OUTPUT

Enter row of length: 5
                      
*         *
**       **
***     ***
****   ****
***** *****

16. create a straight and reverse right-angle in c++

#include<iostream> 
using namespace std; 
 int main()
  {
    int i,j,row;
    cout<<"Enter the number: "; cin>>row;

    cout<<endl;

    for(i=1;i<=row;i++)
     {
       for(j=1;j<=i;j++)
	  cout<<"*";

       cout<<endl;
     }

    for(i=1;i<=row;i++)
     {
       for(j=1;j<row+1-i;j++)
	  cout<<"*";

       cout<<endl;
     }

return 0;
 }

OUTPUT

Enter the number: 5
           
*
**
***
****
*****
****
***
**
*

17. create a butterfly pattern in c++

#include<iostream>
using namespace std;
 
int main()
  {
    int i,j,row;
    cout<<"Enter size: "; cin>>row;

    cout<<endl;

//upper part
     for(i=1;i<=row;i++)
      {
	 for(j=1; j<=i; j++)
	   cout<<"*"; for(int k=row*2; k>i+j-2; k--)
	   cout<<" ";

	 for(j=1; j<=i; j++)
	   cout<<"*";

	 cout<<endl;
      }

// lower part
     for(i=1; i<=row; i++)
      {
	 for(j=1; j<=row-i; j++)
	     cout<<"*";

	 for(int k=0; k<=(i*2); k++)
	     cout<<" ";

	 for(j=1; j<=row-i; j++)
	     cout<<"*";

	 cout<<endl;
      }
   return 0;
 }

OUTPUT

Enter size: 5

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

18. create a diamond pattern in c++

#include<iostream>
using namespace std; 
 int main()
  {
   int row,i,j,k;
   cout<<"Enter the number: "; cin>>row;

   cout<<endl;

//upper part
     for(i=1;i<row;i++)
       {
	 for(j=1;j<=row-i;j++)
	   cout<<" ";

	 for(k=1; k<=i*2-1; k++)
	  {
	    if(i<=row/2)
	         cout<<" ";
	    else if(i==(row/2)+1) 
                 cout<<"*";
	   else if(k==1|| k==i*2-1)
	         cout<<"*";
	   else if(i==row-1) 
                 cout<<" ";
	   else 
                 cout<<" ";
	  }
	cout<<endl;
      }

//lower part
   for(i=1;i<=row;i++)
     {
       for(j=1;j<=i+1;j++)
	   cout<<" ";

       for(k=1; k<=(row-i)*2-3; k++) { if(i>=(row/2)-1) 
             {
	       if(k==1||k==(row-i)*2-3) 
                      cout<<"*"; 
                else 
                      cout<<" ";
             }
          else  if(k==1 || k==(row-i)*2-3)
	      cout<<"*";
	  else
	      cout<<" ";
	 }
       cout<<endl;
     }
return 0;
 }

OUTPUT

Enter the number: 9

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

20. create lattu in c++

#include<iostream.h> 
using namespace std; 
 int main()
  {
   int row,i,j,k;
   cout<<"Enter the number: "; cin>>row;

   cout<<endl;

   int upper = row;
//upper part
     for(i=1;i<upper;i++)
       {
	 for(j=1;j<=upper-i;j++)
	   cout<<" ";

	 for(k=1; k<=i*2-1; k++)
	  {
	    if(i<=upper/2)
	       cout<<" ";
	    else if(i==(upper/2)+1)
	       cout<<"*";
	    else if(k==1|| k==i*2-1)
	       cout<<"*";
	    else if(i==upper-1)
	       cout<<" ";
	    else cout<<" ";
	  }
	 cout<<endl;
      }

//lower part

   int lower=row;
   for(i=1;i<=lower;i++)
     {
       for(j=1;j<=i+1;j++)
	   cout<<" ";

       for(k=1; k<=(lower-i)*2-3; k++)
	   cout<<i;
	 
       cout<<endl;
     }
return 0;
 }

OUTPUT

Enter the number: 9

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

other examples are,

Interested in pattern view Ashish Pandey’blog defficultcoding.com


C++ Examples

Like it?

Leave a Reply

Your email address will not be published. Required fields are marked *