Showing posts with label Pattern. Show all posts
Showing posts with label Pattern. Show all posts

Sunday, 23 September 2018

Printing a rectangular pattern of size i * j in C++

A rectangular pattern of symbols

The rectangular array of symbols is called a rectangular pattern. This pattern contains a certain number of rows and columns. It can contain any numbers, characters or symbols. A rectangular pattern with 4 rows and columns is given below:

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

Write a program in C++ to print a rectangular pattern of size i*j.


We have to input the number of rows and columns the user wants in the pattern. We will use a nested for loop in this program. The first loop will be executed for the number of rows. The second loop will be nested inside the first loop and will execute the number of columns to be printed in that row.

For loop is an iteration loop structure.

Syntax:
for(variable initialization; condition; variable increment or decrement)  
{
statement 1;
statement 2;
.
.
.
}

Program


#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,i,j;
clrscr();
cout<<"Enter the number of rows=";
cin>>a;
cout<<"Enter the number of columns=";
cin>>b;
cout<<"\n";
for (i=1;i<=a;i++)
{
for (j=1;j<=b;j++)
{
cout<<"*";
}
cout<<"\n";
}
getch();
}



Output


Program to print rectangular pattern
PRINTING OF RECTANGULAR PATTERN









Explanation

In the program, the user wants 3 rows and 9 columns. So the outer loop will be executed 3 times (i.e. i=1 to 3). The inner nested loop will be executed 9 times(i.e. j=1 to 9). So we obtain a rectangular pattern of required size.



Printing Floyd's triangle n number of times in C++

Floyd's triangle 

Floyd's triangle is a right-angled triangular array of natural numbers. This triangle was named after Robert Floyd. It is obtained by writing natural numbers serially in right-angled triangular shape. We can also observe that nth row contain n numbers.E.g the fifth row contains five numbers (11,12,13,14,15).


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


Write a program in C++ to print Floyd's triangle with n number of rows. 

Firstly we will take the number of rows the user wants to print in Floyd's triangle. We will use a nested for loop statement to print Floyd's triangle. In this program, we will use two nested for loop. The first loop is used for the execution of the number of rows and the second loop is used for the execution of the number of columns. We will declare a counter with initial value 1 and print it inside the inner for loop(i.e. the loop that executes the no of columns).


Program

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
int i,j,n,count=1;
clrscr();
cout<<"Enter the number of rows=";
cin>>n;
for(i=1;i<=n;i++)
          {
for(j=1;j<=i;j++)
{
cout<<setw(2)<<count<<" ";
count++;
}
     cout<<"\n";
}
getch();
}



Output



Program to print Floyd's triangle
PRINTING OF FLOYD'S TRIANGLE











Explanation

In the program, the user wants to print 6 rows(i.e. n=6). The nested for loop are executed as follows:

i=1 to 6        j=1 to i                   Action                                                count++
1                   j=1 to 1                 
                     j=1                        1 is printed on the screen.                    2
2                   j=1 to 2                   
                     j=1                        2 is printed on the screen.                    3 
                     j=2                        3 is printed on the screen.                    4
3                   j=1 to 3                   
                     j=1                        4 is printed on the screen.                    5 
                     j=2                        5 is printed on the screen.                    6
                     j=2                        6 is printed on the screen.                    7

The process continues till i=6. Finally, we get Floyd's triangle with n number of rows.                     





Printing an inverted pyramid pattern of an asterisk(*) in C++

Printing inverted pyramid of symbols


A pattern is an array formed by repeating, serial or recurring elements. Elements like numbers, symbols or characters may form a pattern. Patterns basically are well organized and systematic. Patterns can be of various shapes and sizes. Basically, in C++ we print shapes like an upright pyramid, inverted pyramid, rectangle, flag, and right-angled triangle. The basic concept used to print pattern in C++ is the use of nested for loop statement.


Write a program in C++ to print an upright pyramid pattern of asterisk.


In this program, we will print an inverted pyramid using for loop statement. To print the inverted pyramid we will need three for loop statements. The first loop is for the execution of no of rows, the second loop is for the printing of spaces and the third loop is for the execution of no of columns. You can use the same method to any other symbols in place of an asterisk(*).

INVERTED PYRAMID

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

Nested loop

If we use a loop inside a loop then it is called a nested loop. It is implemented by considering a new loop as a statement of the previous loop.


Syntax:
for (condition 1)
{
      for(condition 2)
          { 
               for (condition 3)
              {
               .
               .
               }
          }
}



Program


#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
cout<<"Enter the number of rows=";
cin>>n;
for(i=1;i<=n;i++)
     {
          for(k=1;k<i;k++)
          {
          cout<<" ";
          }
          for(j=n;j>=i;j--)
          {
          cout<<"* ";
          }
     cout<<"\n";
     }
getch();
}


Output



Program to print the inverted pyramid.
PRINTING OF INVERTED PYRAMID
                               










Explanation

In this program, the user wanted to print no of rows(i.e. n=6). The following cases occurred:

i=1 to 6                k=1 to (i-1)                                                         j=6 to i
1                          k=1 to 0; No space was printed.                        j=6 to 1
                                                                                                       * was printed six times.
2                          k=1 to 1; One space was printed.                    j=6 to 2
                                                                                                       * was printed five times.
3                          k=1 to 2; Two spaces were printed.                  j=6 to 3
                                                                                                       * was printed four times.

The process continued till i=6 and we obtained the inverted pyramid with 6 rows.






Printing an upright pyramid pattern of an asterisk(*) in C++

Printing upright pyramid of symbols


A pattern is an array formed by repeating, serial or recurring elements. Elements like numbers, symbols or characters may form a pattern. Patterns basically are well organized and systematic. Patterns can be of various shapes and sizes. In C++ we print shapes like the upright pyramid, inverted pyramid, rectangle, flag, and right-angled triangle. The basic concept used to print pattern in C++ is the use of nested for loop statement.


Write a program in C++ to print an upright pyramid pattern of asterisk.


In this program, we will print an upright pyramid using for loop statement. To print the upright pyramid we will need three for loop statements. The first loop is for the execution of no of rows, the second loop is for the printing of spaces and the third loop is for the execution of no of columns. You can use the same method for any other symbols in place of an asterisk(*).


UPRIGHT PYRAMID

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

Nested loop

If we use a loop inside a loop then it is called a nested loop. It is implemented by considering a new loop as a statement of the previous loop.


Syntax:
for (condition 1)
{
      for(condition 2)
          { 
               for (condition 3)
              {
               .
               .
               }
          }
}


Program


#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
cout<<"Enter number of rows of pyramid= ";
cin>>n;
for(i=1;i<=n;i++)        
     {
     for(k=n;k>i;k--)
           {
           cout<<" ";
           }
      for(j=1;j<=i;j++)
          {
          cout<<"* ";
          }
          cout<<"\n";
     }
getch();
}


Output


Program to print upright pyramid with n number of rows
PRINTING OF AN UPRIGHT PYRAMID










Explanation

In this program, the user wanted to print 6 no of rows(i.e. n=6). The following cases occurred:

i=1 to 6                k=6 to (i+1)                                                         j=1 to i
1                          k=6 to 2; Five spaces were printed.                  j=1 to 1
                                                                                                       * was printed once.
2                          k=6 to 3; Four spaces were printed.                 j=1 to 2
                                                                                                       * was printed twice.
3                          k=6 to 4; Three spaces were printed.               j=1 to 3
                                                                                                       * was printed thrice.

The process continued till i=6 and we obtained the upright pyramid with 6 rows.


Fact about c++

C++ programming language was also called 'The New C' when it was introduced.



Saturday, 22 September 2018

Printing of Pascal's triangle in C++


Pascal's triangle 

Pascal's triangle is a mathematical triangle named after French mathematician Blaise Pascal. This triangle consists of different numbers and has infinite numbers of rows. The main arithmetic followed to create this triangle is the addition. The number of elements in a row is equal to the row on which the elements are placed on. The following triangle is the example of Pascal's triangle having five rows:


                       1
                    1    1
                 1      1
              1    3    3    1
           1    4    6    4    1


The first row in triangle contains only one number i.e 1. In the second row, the first element 1 is the sum of its top left and right element i.e 0 and 1. Similarly, the second element 1 of the second row is the sum of its top left and right element i.e 1 and 0. In the third row first element is 1 which is the sum of its top left and right numbers i.e 0 and 1. The second element on the third row is 2 which is the sum of its top left and right number i.e 1 and 1. A similar process is carried out to find elements in other rows of the triangle.



Write a program in C++ to print Pascal's triangle with n number of rows.


We will ask the user about the number of row s/he wants to print.We will use a matrix to create the triangle.The first and last element of each row is declared as 1.The the elements a[i][j] in between are calculated by the sum of top left a[i-1][j-1] and top right element a[i-1][j] as:

a[i][j]=a[i-1][j-1]+a[i-1][j];

Then we print the matrix.


Program

#include<iostream.h>
#include<conio.h>
void main()
{
int i,j,k,n,x,y,z;
int a[20][20];
clrscr();
cout<<"Enter a number of rows= ";
cin>>n;
for(k=1;k<=n;k++)                                     //Loop for declaration of first and last element of row as 1
{                                                               
a[k][1]=1;                                                               
a[k][k]=1;                                                                         
}                                                                                       
for(i=3;i<=n;i++)                                      /
/Loop for calculation of middle elements of row    
{
for(j=2;j<i;j++)
{
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
}
for(x=1;x<=n;x++)                                   //Loop for printing of matrix       
{                                                                               
for(z=n;z>x;z--)                                //Loop for manipulation of spaces                             
{
cout<<"  ";
}
for(y=1;y<=x;y++)
{
cout<<"  "<<a[x][y];
}
cout<<"\n";
}
getch();
}


Output

Program to print Pascal's triangle
PRINTING OF PASCAL'S TRIANGLE










Explanation

In the above program, the user wanted to print Pascal's triangle with 5 number of rows. The numbers were stored in an array and addition was performed as per the method discussed above. Pascal's triangle with 5 rows was displayed on the output screen.


Fact about C++

The output screen(i.e black in color) in C++ programming is also called Console screen.