Basic Patterns in C++

PATTERNS PROGRAMS IN C++

create a Rectangle of number of ascending order in C++

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

create-a-square-of-numbers-of-ascending-and-descending-order-in-cpp

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

#include<iostream.h>
#include<conio.h>

 void main()
  {
    int row,i,j;
    clrscr();
    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++;
      }

   getch();
 }
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

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

#include<conio.h>
#include<iostream.h>

 void main()
  {
    int row,i,j;
    clrscr();
    cout<<"Enter the number: "; cin>>row;

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

   getch();
 }
OUTPUT:-

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

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

#include<iostream.h>
#include<conio.h>

 void main()
  {
   int row,i,j,sum,cell=1;
   clrscr();
   cout<<"Enter Number or rows: "; cin>>row;

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

   getch();
 }
OUTPUT:-
Enter Number of rows: 5

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

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

#include<iostream.h>
#include<conio.h>

 void main()
  {
   int row,i,j,k;
   clrscr();
   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;
    }

   getch();
 }
OUTPUT:-
Enter number of row: 5
           
         1
       1 2
     1 2 3
   1 2 3 4
 1 2 3 4 5

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

#include<iostream.h>
#include<conio.h>

 void main()
  {
   int row,i,j,k;
   clrscr();
   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;
     }

   getch();
 }
OUTPUT:-
Enter Number of rows: 6

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

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

#include<iostream.h>
#include<conio.h>

 void main()
  {
    int row,i,j;
    clrscr();
    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;
      }

   getch();
 }
OUTPUT:-

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

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

#include<iostream.h>
#include<conio.h>

 void main()
  {
    int row,i,j;
    clrscr();
    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;
      }

   getch();
 }
OUTPUT:-

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

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

#include<iostream.h>
#include<conio.h>

 void main()
  {
   int row,x=1;
   clrscr();
   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++;
     }
   getch();
 }
 OUTPUT:-
Enter the number: 5

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

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

#include<iostream.h>
#include<conio.h>

 void main()
  {
   int row,x=1;
   clrscr();
   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++;
     }
   getch();
 }
OUTPUT:-
Enter the number: 5

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

create a complete right-angle of only ascending number

#include<iostream.h>
#include<conio.h>

 void main()
  {
    int row,i,j,count=1;
    clrscr();
    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;
      }

   getch();
 }
OUTPUT:-
Enter the number: 5

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

create a right-angle of odd numbers

#include<iostream.h>
#include<conio.h>

 void main()
  {
    int row,i,j,count=1;
    clrscr();
    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;
      }

   getch();
 }
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

create Pyramid of numbers as ascending and descending in C++

#include<iostream.h>
#include<conio.h>

 void main()
  {
    int i,j,k,l,n;
    clrscr();
    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;
      }

    getch();
  }
OUTPUT:-
Enter row of Number: 5

    1   
   121 
  12321  
 1234321
123454321

Making a right-angle in different way using alphabet character

#include<iostream.h>
#include<conio.h>

 void main()
  {
    clrscr();
    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++;
      }

   getch();
 }
OUTPUT:-
Enter Number: 5

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

#include<iostream.h>
#include<conio.h>

 void main()
  {
    clrscr();
    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;
      }

   getch();
 }
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

creating a right-angle using ascii code

#include<iostream.h>
#include<conio.h>

 void main()
  {
    clrscr();

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

   getch();
 }
OUTPUT:-
 A
 A B
 A B C
 A B C D
 A B C D E