complex Patterns

PATTERN PROGRAMS IN C++
  • create a solid square in C++

    #include<iostream.h
    #include<conio.h
    
     void main()
      {
       int i,j,n;
       clrscr();
       cout<<"Enter Number of rows: "; 
       cin>>n;
    
        for(i=1; i<=n; i++)
         {
           int j=1;
           while(j<=n*2)
    	{
    	     cout<<"*";
    	     j++;
           }
           cout<<endl;
         }
    
        getch();
      }
    OUTPUT:-
    Enter Number of rows: 5
    
    **********
    **********
    **********
    **********
    **********

    create a Pyramid in C++

    #include<iostream.h>
    #include<conio.h>
    
     void main()
     {
       int row;
       clrscr();
       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;
         }
    
       getch();
     }
    OUTPUT:-
    Enter number of rows: 5
    
        *
       ***
      *****
     *******
    *********

    Here is another one,

    #include<iostream.h>
    #include<conio.h>
    
     void main()
      {
        int row,count=1;
        clrscr();
        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;
         }
      getch();
     }
    OUTPUT:-
    
    Enter Number of rows: 5
        1       
       *2* 
      **3**  
     ***4*** 
    ****5****  

     

    creating a reverse pyramid in C++

    #include<iostream.h>
    #include<conio.h>
    
     void 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;
          }
    
       getch();
     }
    OUTPUT:-
    Enter number of row: 5
     *********
      *******
       *****
        ***
         *

    Here is another one,

    #include<iostream.h>
    #include<conio.h>
    
     void main()
      {
       int row,i,j,k;
       clrscr();
       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++;
        }
       getch();
     }
    OUTPUT:-
    Enter Number of rows: 6
    
     * * * * *
      * * * *
       * * *
        * *
         *  

     

    create a  straight pyramid and reverse pyramid 

    #include<iostream.h>
    #include<conio.h>
    
     void main()
      {
       int row,i,j,k;
       clrscr();
       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;
         }
    
       getch();
     }
    OUTPUT:-
    Enter Number of rows: 5
              
        *  
       *** 
      *****  
     ******* 
    ********* 
     *******
      *****    
       ***   
        *

     

    create triangle with other reverse triangle

    #include<iostream.h>
    #include<conio.h>
    
     void main()
      {
       int row,i,j,k;
       clrscr();
       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;
         }
    
       getch();
     }
    OUTPUT:-
    Enter the number: 5
                  
        *  
       * *  
      *   * 
     *     * 
    *       * 
     *     *
      *   *
       * *
        *

    create a Isoscles Triangle in C++

    #include<iostream.h>
    #include<conio.h>
    
     void main()
      {
        int i,j,k,row;
        clrscr();
        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;
          }
    
       getch();
     }
    OUTPUT:-
    Enter Number of rows: 5
    
        * 
       * * 
      *   * 
     *     *
    *********

     

    create a Equilateral Triangle in C++

    #include<iostream.h>
    #include<conio.h>
    
     void main()
     {
       int row;
       clrscr();
       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;
         }
       getch();
     }
    OUTPUT:-
    Enter number of rows: 5
    
             *  
           *   *
         *       *
       *           *
     * * * * * * * * *

    Here is another one,

    #include<iostream.h>
    #include<conio.h>
    
     void main()
     {
       int row;
       clrscr();
       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++;
         }
       getch();
     }
    OUTPUT:-
    Enter number of rows: 5
    
            /\
           /  \
          /    \
         /      \
        /________\

     

    create a right-angle in C++

    #include<iostream.h>
    #include<conio.h>
    
     void main()
      {
       int row,i,j;
       clrscr();
       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++;
        }
    
       getch();
     }
    OUTPUT:-
    Enter the number: 5
    *
    **
    * *
    *  *
    *****

     

    create a Obtuse Triangle in C++

    #include<iostream.h>
    #include<conio.h>
    
     void main()
      {
        int row,i,j,s;
        clrscr();
        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++;
          }
    
       getch();
     }
    OUTPUT:-
    Enter size: 9
    
      *
       * *
        *   *
         *     *
          *********

     

    create a Rectangle in C++

    #include<iostream.h>
    #include<conio.h>
    
     void main()
      {
       int i,j,row;
       clrscr();
       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;
         }
    
        getch();
      }
    OUTPUT:-
    
    Enter Number of rows: 5
    
    ***** 
    *   * 
    *   * 
    *   * 
    *****

     

    create a square in C++

    #include<iostream.h>
    #include<conio.h>
    
     void main()
      {
       int i,j,row;
       clrscr();
       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;
         }
        getch();
      }
    OUTPUT:-
    
    Enter Number of rows: 5
    **********
    *        *
    *        *  
    *        *  
    **********

    create Hexagon in C++

    #include<iostream.h>
    #include<conio.h>
    
     void main()
      {
       int row,i,j,k;
       clrscr();
       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;
         }
       getch();
     }
    OUTPUT:-
     Enter the number: 8
    
       *********
      *         *
     *           *
      *         *
       *********

    create more than one Pyramids in C++

    #include<iostream.h>
    #include<conio.h>
    
     void main()
      {
        int i,j,k,l,row,column;
        clrscr();
        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;
           }
    
       getch();
     }
    OUTPUT:-
    Enter Height of Pyramid: 5
    Enter Number of Pyramid: 6  
                                                                                    
        *        *        *        *        *        * 
       ***      ***      ***      ***      ***      *** 
      *****    *****    *****    *****    *****    *****
     *******  *******  *******  *******  *******  *******
    ******************************************************

    create more than one  Triangles in C++

    #include<iostream.h>
    #include<conio.h>
    
     void main()
      {
        int i,j,k,l,row,column;
        clrscr();
        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;
           }
       getch();
     }
    OUTPUT:-
    Enter Height of Pyramid: 4
    Enter Number of Pyramid: 4
                                                                                    
          *             *             *             *
        *   *         *   *         *   *         *   *
      *       *     *       *     *       *     *       *
    * * * * * * * * * * * * * * * * * * * * * * * * * * * *

     

    create reverse to straight Pyramid in C++

    #include<iostream.h>
    #include<conio.h>
    
     void main()
      {
        int row,i,k,l,j;
        clrscr();
        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;
         }
    
       getch();
     }
    OUTPUT:-
    Enter row of NumberX2: 5
                                                                                    
    ********* 
     ******* 
      *****
       ***
        * 
        * 
       *** 
      ***** 
     ******* 
    *********

     

    create right-angle from left and right side 

    #include<iostream.h>
    #include<conio.h>
    
     void main()
      {
        int n,i,j,k,l;
        clrscr();
        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;
          }
    
        getch();
     }
    OUTPUT:-
    Enter row of length: 5
                                                                                    
    *         *
    **       **
    ***     ***
    ****   ****
    ***** *****
    
    

     

    create a straight and reverse right-angle in C++

    #include<iostream.h>
    #include<conio.h>
    
     void main()
      {
        int i,j,row;
        clrscr();
        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;
         }
    
       getch();
     }
    OUTPUT:-
    Enter the number: 5
                                                                                    
    *
    **
    ***
    ****
    *****
    ****
    ***
    **
    *

     

    create a butterfly in C++

    #include<iostream.h>
    #include<conio.h>
    
     void main()
      {
        int i,j,row;
        clrscr();
        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;
          }
       getch();
     }
    OUTPUT:-
    Enter size: 5
    
    *         *
    **       **
    ***     ***
    ****   ****
    ***** *****
    ****   ****
    ***     ***
    **       **
    *         *
    

     

    create a Diamond in C++

    #include<iostream.h>
    #include<conio.h>
    
     void main()
      {
       int row,i,j,k;
       clrscr();
       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;
         }
       getch();
     }
    OUTPUT:-
    Enter the number: 9
    
        *********
       *         *
      *           *
     *             *
      *           *
       *         *
        *       *
         *     *
          *   *
           * *
    	*

     

    create lattu in C++

    #include<iostream.h>
    #include<conio.h>
    
     void main()
      {
       int row,i,j,k;
       clrscr();
       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;
         }
       getch();
     }
    OUTPUT:-
    Enter the number: 9
    
        *********
       *         *
      *           *
     *             *
      *************
       ***********
        *********
         *******
          *****
           ***
            *

    other are,