Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

C++ Pattern Programs

Print triangle using "*", numbers, and characters

We shall employ two nested loops with iterable variables like I and j in order to print a triangle pattern programme in C++. The following is the complete reasoning for printing a triangle using x, where x can be a letter, number, or *:

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

When we put the aforementioned logic into practice, the inner loop will print x once in the first row, twice in the second row, and so forth.

Tips: The first for loop simply needs to be changed so that we iterate from i=rows – 1 to i=0 in order to output the inverted triangle, and we are good to go.

Code 1: Printing half pyramid using *

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

Code Implementation

#include<bits/stdc++.h>
using namespace std;

int main(){

    int rows;
    cin >> rows;
    for( int i = 0; i < rows; i++ ) {
        for( int j = 0; j <= i; j++ ){
            cout << "*  ";
        }
        cout<<endl;
    }
    
    return 0;
}

Output

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

Code 2: Printing half pyramid using numbers

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

Code Implementation

#include<bits/stdc++.h>
using namespace std;

int main(){

    int rows;
    cin >> rows;
    for( int i = 0; i < rows; i++ ) {
        for( int j = 0; j <= i; j++ ){
            cout << i + 1 << " ";
        }
        cout<<endl;
    }

    return 0;
}

Output

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

Code 3: Printing half pyramid using Alphabets

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

Code Implementation

#include<bits/stdc++.h>
using namespace std;

int main(){
    int rows;
    cin >> rows;
    for( int i = 0; i < rows; i++ ) {
        for( int j = 0; j <= i; j++ ){
            cout << (char)('A' + j) << " ";
        }
        cout<<endl;
    }

    return 0;
}

Output

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

Printing rectangle pattern

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

The logic is as follows :

  • Consider the dimensions (rows and columns) of the rectangle as input. Allow for rows and columns.
  • Run the following code in iterations from i=0 to i=rows:
  • Iterate from j=0 to j=cols and print x + space each time i.ei.e cout << x << " ";
  • Terminate the current line, i.e. cout << endl;

Code Implementation to print rectangle in C

#include<bits/stdc++.h>
using namespace std;

int main(){

    int rows, cols;
    cin >> rows;
    cin >> cols;
    for( int i = 0; i < rows; i++ ) {
        for( int j = 0; j < cols; j++ ){
            cout << "* ";
        }
        cout<<endl;
    }

    return 0;
}

Output

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

Printing 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=0 to i=rows and do the following :
  • Iterate from j=0 to j=cols and do the following :
  • If current is the border cell of a rectangle i.e.i.e. i = 0 OR i = rows – 1 OR j = 0 OR j = cols – 1 print * + space.
  • Otherwise, print blank space.

Code Implementation for Printing Hollow Rectangle Pattern

#include<bits/stdc++.h>

using namespace std;

int main() {

  int rows, cols;
  cin >> rows;
  cin >> cols;

  for (int i = 0; i < rows; i++) {
    for (int j = 0; j < cols; j++) {
      if (i == 0 || i == rows - 1 || j == 0 || j == cols - 1)
        cout << "* ";
      else
        cout << "  ";
    }
    cout << endl;
  }

  return 0;
}

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 rows.
  • Define a variable, say counter and initialize its value to 0.
  • Iterate for i=0 to i=rows-1 and do :
  • Iterate for j=0 to j=rows and do the following :
  • Print count + space each time i.e. cout << count << " ";
  • Increase the value of count by 1 i.e. count++.
  • Terminate the current line, i.e. cout << endl;

Code Implementation for Floyd Triangle

#include<bits/stdc++.h>
using namespace std;

int main(){
    int rows;
    cin >> rows;
    int counter = 1; 
    for( int i = 0; i < rows; i++ ) {
        for( int j = 0; j <= i; j++ ){
            cout << (counter++) << " ";
        }
        cout << endl;
    }
    
    return 0;
}

Output

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 

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 rows.
  • Iterate from i=0 to i=rows-1 and do the following –
  • Iterate from j=0 to j=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×(rows−i).
  • Iterate from j=0 to j=spaces−1 and print blank spaces.
  • Iterate from j=0 to j=i and print + Blank space i.e. cout << "" << " ";
  • Terminate the line, i.e.i.e. count << endl;
  • Repeat the above-given steps, but this time Iterate from i=rows−1 to i=0. This will print the lower part, as we have already printed the upper part.

Code Implementation for Butterfly Pattern

#include<bits/stdc++.h>
using namespace std;

int main(){

    int rows;
    cin >> rows;
    
    for( int i = 0; i <= rows; i++ ){
        for( int j = 0; j <= i; j++ ){
            cout << "* ";
        }
        int spaces = 2 * (rows - i);
        for( int j = 0; j < spaces; j++){
            cout << "  ";
        }
        for( int j = 0; j <= i; j++ ){
            cout << "* ";
        }
        cout << endl;
    }
    
    for( int i = rows - 1; i >= 0; i-- ){
        for( int j = 0; j <= i; j++ ){
            cout << "* ";
        }
        
        int spaces = 2 * (rows - i);
        for( int j = 0; j < spaces; j++){
            cout << "  ";
        }
        
        for( int j = 0; j <= i; j++ ){
            cout << "* ";
        }

        cout << endl;
    }

    return 0;
}

Output

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

Full Pyramid

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 rows.
  • Iterate from i=0 to i=rows – 1 and do the following :
  • Define a variable to find the number of spaces needed to be printed. Say it to be space.
  • Initialize its value to be rows – i.
  • Iterate from j=0 to j=space – 1 and print double spaces.
  • Iterate from j=0 to j=2×i−1 and print *+space.
  • Terminate the line i.e. cout << endl;

C Code Implementation for Full Pyramid

#include<bits/stdc++.h>
using namespace std;

int main(){

    int rows;
  
    cin >> rows;
    for( int i = 0; i < rows; i++ ) {
        int spaces = rows - i;

        for( int j = 0; j < spaces; j++){
            cout <<"  ";
        }
        for( int j = 0; j < 2 * i - 1; j++){
            cout <<"* ";
        }

        cout << endl;

    }
    
    return 0;
}

Output

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

Conclusion
All kinds of patterns can be easily printed by correctly using nested for loops (or while/do-while loops). To make it easier, code can be split into two parts, just like in the Butterfly pattern. In some pattern programs in C++, we should take utmost care of blank spaces to make the pattern.

Other C++ Programs

C++ Program to Add Two Numbers
C++ Program to Reverse a Number
C++ program for heap sort
Bubble Sort in C++ with Program Code and Example
Hello world program in C++
Prime Number Program in C++
Factorial Program in C++
Binary Search Program in C++

Leave a Reply

Your email address will not be published. Required fields are marked *