Basic Patterns Program in C++

Here in this Page you found all Pattern Program list in C++, which are divided into two category Basic Patterns in C++ and Complex Patterns in C++,

[lwptoc numeration=”none”]


But make before creating patterns in C++ you read create patterns in C++ you can also view ashish Pandey ‘s Blog DIFFICULTCODING.COM where he will teach you about patterns programs in C++

Let’s start here,

Basic Pattern Program List In C++

1. create a rectangle of number of ascending order in C++

#include<iostream>
using namespace std;

 int main()
  {
    int row,i,j;
    cout<<"Enter number of row: "; 
    cin>>row;
    
     cout<<endl;

     i=1;
     while(i<=row) // row
       {
         for(j=1; j<=row; j++)  // column
	    {
	       cout<<" "<<j;    
	    }
	    cout<<endl;
	    i++;
       }

return 0;
 }

OUTPUT

Enter number of row : 5

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

2.create a square of numbers of ascending and descending order in C++

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

     i=1;
     while(i<=row) // row of number
      {
	if(i%2!=0)
	  {
	    for(j=1; j<=row+1; j++)
	       cout<<" "<<j; } else { for(j=row; j>=0; j--)
	       cout<<" "<<j;
	  }
	  cout<<endl;
	  i++;
      }

return 0;
 }

OUTPUT

Enter number of row: 5

 1 2 3 4 5 6
 5 4 3 2 1 0
 1 2 3 4 5 6
 5 4 3 2 1 0
 1 2 3 4 5 6

3.create a right-angle of numbers of descending order as horizontally

<

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

     for(i=row; i>=1; i--) // row
      {
	for(j=row; i<=j; j--)  // column
	 {
	   cout<<" "<<j;
	 }
	cout<<endl;
      }
return 0;
 }

OUTPUT

Enter the number: 5
 5  
 5 4 
 5 4 3
 5 4 3 2
 5 4 3 2 1

4.create a right-angle of numbers of ascending order as vertically

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

   for(i=1; i<=row; i++)
    {
      for(j=1;j<=i;j++)
       {
	 cout<<cell<<" ";
       }
      cout<<endl;
      cell++;
    }

return 0;
 }
Enter Number of rows: 5

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

5.create a right-angle of numbers of ascending order from right-side

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

   cout<<endl;

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

       for(k=1; k<=i; k++)
	 cout<<k;

       cout<<endl;
    }

return 0;
 }

OUTPUT

Enter number of row: 5
           
         1
       1 2
     1 2 3
   1 2 3 4
 1 2 3 4 5

6. create a reverse right-angle of numbers from right-side

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

   cout<<endl;

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

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

       cout<<endl;
     }
return 0;
 }

OUTPUT

Enter Number of rows: 6

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

7. create a reverse right-angle of numbers from left-side

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

     for(i=row; i>=1; i--) // row
      {
	for(j=1; j<=i; j++) // column 5>=1 1<=j
	 {
	   cout<<" "<<j;
	 }
	cout<<endl;
      }

return 0;
 }

OUTPUT

Enter the number: 5
 1 2 3 4 5  
 1 2 3 4
 1 2 3
 1 2
 1

8. create a reverse right-angle of descending numbers as horizontally from left-side

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

     for(i=1; i<=row; i++) // row
      {
	for(j=row; i<=j; j--) // column 5>=1 1<=j
	 {
	   cout<<" "<<j;
	 }
	cout<<endl;
      }

return 0;
 }

OUTPUT

Enter the number: 5

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

9. create a reverse right-angle of ascending numbers as vertically from left-side

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

    for(int i=row; i>=1; i--) //for(int i=1;i<=n;i++)
     {
       for(int j=1; j<=i; j++) //for(int j=1;j<=n+1-i;j++)
	{
	   cout<<x<<" ";
	}
	cout<<endl;
	x++;
     }
return 0;
 }

OUTPUT

Enter the number: 5

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

10. create a reverse right-angle of descending numbers as vertically from left-side

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

    for(int i=row; i>=1; i--) //for(int i=1;i<=n;i++)
     {
       for(int j=1; j<=i; j++) //for(int j=1;j<=n+1-i;j++)
	{
	   cout<<x<<" ";
	}
	cout<<endl;
	x++;
     }
 return 0;
 }

OUTPUT

Enter the number: 5

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

11. create a complete right-angle of only ascending number

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

     for(i=1; i<=row; i++) // row
      {
	for(j=1;j<=i;j++)  // column
	 {
	   cout<<" "<<count++;    // print j value
	 }
	cout<<endl;
      }

return 0;
 }

OUTPUT

Enter the number: 5

 1
 2 3
 4 5 6
 7 8 9 10
 11 12 13 14 15

12. create a right-angle of odd numbers

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

     for(i=1; i<=row; i++) // row
      {
	for(j=1;j<=i;j++)  // column
	 {
	   cout<<" "<<count;    // print j value
	   count=count+2;
	 }
	cout<<endl;
      }

return 0;
 }

OUTPUT

Enter the number: 5

 1
 3 5 
 7 9 11
 13 15 17 19
 21 23 25 27 29

For even numbers, Only change variable count value to 0, while remaining code will be the same as above code which will print following pattern,

Enter the number: 5

 0
 2 4
 6 8 10
 12 14 16 18
 20 22 24 26 28

13. create pyramid of numbers as ascending and descending in c++

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

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

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

	cout<<endl;
      }

return 0;  }

OUTPUT

Enter row of Number: 5

    1   
   121 
  12321  
 1234321
123454321

14. create a right-angle in different way using alphabet character

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

     for(int i =1; i<=row; i++)
      {
	for(int j=1;j<=i;j++)
	 {
	    cout<<char(count)<<" ";
	 }
	 cout<<endl;
	 count++;
      }

return 0;
 }

OUTPUT

Enter Number: 5

 A
 B B
 C C C
 D D D D
 E E E E E

15.create a right angle in different way using alphabet character

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

     for(int i = 0; i<=row; i++)
      {
	cout<<char(count++)<<" ";

	for(int j=1; j<=i; j++)
	 {
	    cout<<char(count++)<<" ";
	 }
	 cout<<endl;
      }

return 0;
 }

OUTPUT

Enter Number: 5

 A
 B C
 D E F
 G H I J
 K L M N O
 P Q R S T U

16. creating a right-angle using ascii code

#include<iostream> 
using namespace std;
 
 int main()
  {
     for(int i = 65; i<70; i++)
      {
	for(int j=65; j<=i; j++)
	 {
	    cout<<char(j)<<" ";
	 }
	 cout<<endl;
      }

return 0;
 }

OUTPUT

 A
 A B
 A B C
 A B C D
 A B C D E

more about patterns,


C++ Examples