Pattern Programming in C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Introduction

Pattern programming means printing different patterns, such as triangles, pyramids, rectangles, etc., using a programming language.

Pattern programming in C++ requires a decent knowledge of loops or, more specifically, nested loops. Writing code(s) to print patterns using special characters, numbers, and alphabets makes one understand how the loops work internally.

  • In this article, the code to print triangles, pyramids, and rectangles using *, numbers, and alphabets has been shown.
  • Other complex patterns, like the Butterfly diagram and Pascal's triangle, have also been shown.

Top C++ Pattern Programs:

  • Print triangles using *, numbers, and characters
  • Rectangle Pattern
  • Hollow Rectangle Pattern
  • Inverted Half Pyramid
  • Floyd’s Triangle
  • Butterfly Pattern
  • Print Pascal's triangle
  • Print Full Pyramid Pattern of Stars
  • Print Pattern of Stars
  • C++ Print Triangle Star Pattern
  • C++ Print Pattern of Numbers

C++ Programs for Important Patterns

To print a triangle pattern program in C++, we will use two nested loops with iterable variables as i and j. The whole logic to print a triangle using x is as follows, where x can be *, number, and character:

  • Take input from the user of the number of rows in the triangle. Let it be rows.
  • Iterate for i=0i=0 to i=rows1i=rows-1 and do :
    • Iterate for j=0j=0 to j=rowsj=rows and print xx + space each time i.e cout << x << " ";
    • Terminate the current line, i.e. cout << endl;

When we implement the above logic, the inner loop will print x 1 time in the first row, then two times in the second row, and so on.

Note: if we want to print the inverted triangle, the only change that is required is to change the first for loop such that we iterate from i=rows1i=rows - 1 to i=0i=0, and we are good to go.

Code 1: Printing half pyramid using *

To print a half pyramid/triangle using *, we will replace the xx with *, and we are good to go.

Source Code:

Output:

Code 2: Printing half pyramid using numbers

Again to print a pyramid of numbers, we need to replace xx with i+1i+1, where i+1i+1 denotes the row number. Here it is i+1i+1 because we are starting ii from 0.

Source Code:

Output:

Code 3: Printing half pyramid using Alphabets

Similarly, we will replace the xx with the desired alphabets to print a pyramid of alphabets.

Source Code:

Output:

Rectangle pattern

Coding a rectangle pattern program in C++ is similar to printing a matrix of dimensions n×mn\times m.

The logic is as follows :

  • Take the input of rows and columns (dimensions) of the rectangle. Let them be rows and cols.
  • Iterate from i=0i=0 to i=rowsi=rows and do the following :
    • Iterate from j=0j=0 to j=colsj=cols and print xx + space each time i.ei.e cout << x << " ";
    • Terminate the current line, i.e.i.e. cout << endl;

Source Code:

Output :

Hollow Rectangle Pattern

The process to code the hollow rectangle pattern program in C++ is almost the same as printing a normal rectangle with the only difference that while printing a hollow rectangle, we need to take care of printing empty spaces for non-border cells.

The logic is as follows :

  • Take the input of rows and columns (dimensions) of the rectangle. Let them be rows and cols.
  • Iterate from i=0i=0 to i=rowsi=rows and do the following :
    • Iterate from j=0j=0 to j=colsj=cols and do the following :
      • If current is the border cell of rectangle i.e.i.e. i = 0 OR i = rows - 1 OR j = 0 OR j = cols - 1 print * + space.
      • Otherwise, print blank space.

Source Code:

Output:

Floyd's triangle

In Floyd's triangle, we need to print numbers, but it is also required to increase the number by 1 each time we print it. Floyd's Triangle is similar to the half triangle using numbers, with the only difference being that in Floyd's triangle, we need to maintain a counter which will be incremented by one each time it is printed. The logic is as follows :

  • Take input from the user of the number of rows in the triangle. Let it be rowsrows.
  • Define a variable, say countercounter and initialize its value to 0.
  • Iterate for i=0i=0 to i=rows1i=rows-1 and do :
    • Iterate for j=0j=0 to j=rowsj=rows and do the following :
      • Print countcount + space each time i.ei.e cout << count << " ";
      • Increase the value of count by 1 i.e.i.e. count++.
    • Terminate the current line, i.e.i.e. cout << endl;

Source Code:

Output:


Butterfly Pattern

To easily print the butterfly pattern in C++, we will split our code into two parts, i.e., print the upper part in the first go and then the second part.

The logic is as follows :

  • Take input from the user of the number of rows in the Butterfly pattern. Let it be rowsrows.
  • Iterate from i=0i=0 to i=rows1i=rows-1 and do the following -
    • Iterate from j=0j=0 to j=ij = i and print *+ Blank space i.e.i.e. cout << "*" << " ";
    • Define a variable to find the number of spaces needed to be printed; say it to be spacespace.
    • Initialize its value to be 2×(rowsi)2\times (rows - i).
    • Iterate from j=0j=0 to j=spaces1j=spaces-1 and print blank spaces.
    • Iterate from j=0j=0 to j=ij = i and print *+ Blank space i.e.i.e. cout << "*" << " ";
    • Terminate the line, i.e.i.e. count << endl;
  • Repeat the above-given steps, but this time Iterate from i=rows1i=rows - 1 to i=0i=0. This will print the lower part, as we have already printed the upper part.

Source Code :

Output:

Pascal's triangle is a triangle that consists of a binomial coefficient i.e.i.e. (nr)\binom{n}{r} (number of ways to choose rr things elements a set of nn elements). We can easily find the value of (nr)\binom{n}{r} using pascal's triangle by looking at the rthr^{th} value in the nthn^{th} row.

The logic for printing pascal's triangle is as follows :

  • Take input from the user of the number of rows in the triangle. Let it be rowsrows.
  • Iterate from i=0i=0 to i=rows1i=rows-1 and do the following :
    • Print rowsirows - i spaces using a for a loop.
    • Define a variable, say coefficient.
    • Iterate from j=0j=0 to j=ij=i and do the following :
      • If j==0j==0, then set coefficient's value to be 1.
      • Otherwise multiply (i - j + 1) to coefficient and divide it by j i.e.i.e. coefficient * = (i - j + 1) / j;
      • Print coefficient + space i.e. cout << coefficient << " ";
    • Terminate the line i.e.i.e. cout << endl;

Source Code:

Output:

Printing the full pyramid is almost the same as printing the half pyramid. We need to print space before printing the *s. The logic is as follows :

  • Take input from the user of the number of rows in the triangle. Let it be rowsrows.
  • Iterate from i=0i=0 to i=rows1i=rows - 1 and do the following :
    • Define a variable to find the number of spaces needed to be printed. Say it to be spacespace.
    • Initialize its value to be rowsirows - i.
    • Iterate from j=0j=0 to j=space1j=space - 1 and print double spaces.
    • Iterate from j=0j=0 to j=2×i1j=2\times i - 1 and print *+space.
    • Terminate the line i.e. cout << endl;

Source Code:

Output:

The following code contains some of the pattern programs in c++ combined, which we have already seen in the previous sections of this article.

Source Code:

Output:

C++ Print Triangle Star Pattern

1. Simple triangle.

Code

Output

2. Inverted triangle.

Code

Output

C++ Print Pattern of Numbers

Code

Output

Conclusion

  1. All kinds of patterns can be easily printed by correctly using nested for loops (or while/do-while loops).
  2. i and j are the variables commonly used in nested loops to print patterns.
  3. To make it easier, code can be split into two parts, just like in the Butterfly pattern.
  4. In some pattern programs in C++, we should take utmost care of blank spaces to make the pattern.