C++ Program To Print Triangle Pattern

Last Updated : 16 Jan, 2026

Here we will see how to print triangle patterns using a C++ program. There are 5 patterns discussed here:

  • Right Triangle.
  • Inverted Right Triangle.
  • Equilateral Triangle.
  • Inverted Equilateral Triangle.
  • Inverted Mirrored Right Triangle.

1. Right Triangle

Example for the right triangle:

Input: 4
Output: 
*
* *
* * *
* * * *

In the above pattern, it can be observed that ith row has i elements.

C++
#include <iostream>
using namespace std;

int main(){
    int n = 5;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++)
            cout << "* ";
        cout << endl;
    }
    return 0;
}

Output
* 
* * 
* * * 
* * * * 
* * * * * 

Explanation:

  • int n = 5, sets the number of rows in the pattern.
  • for (int i = 1; i<= n; i++) is the outer loop that controls how many rows are printed.
  • for (int j = 1; j<= i; j++) is the inner loop that prints stars equal to the current row number.

2. Inverted Right Triangle

Example of inverted right triangles:

Input: 5
Output:
* * * * *
* * * *
* * *
* *
*

In this example, it can be observed that if there are a total of n rows in the triangle then ith row has n-i+1 elements.

C++
#include <iostream>
using namespace std;

int main(){
    int n = 5;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i + 1; j++)
            cout << "* ";
        cout << endl;
    }
    return 0;
}

Output
* * * * * 
* * * * 
* * * 
* * 
* 

Explanation:

  • int n = 5, defines the total number of rows in the pattern.
  • for (int i = 1; i<= n; i++) is the outer loop that controls the number of rows printed.
  • for (int j = 1; j<= n-i+1; j++) is the inner loop that prints stars in decreasing order for each row.
  • With each new row, the number of stars reduces by one.

Another Approach: This program uses a reverse outer loop (from n to 1) to directly generate the inverted right-angled triangle by decreasing the star count in each iteration.

C++
#include <bits/stdc++.h>
using namespace std;
int main(){
    int n = 5;
    for (int i = n; i >= 1; i--) {
        for (int j = i; j >= 1; j--)
            cout << "* ";
        cout << endl;
    }
    return 0;
}

Output
* * * * * 
* * * * 
* * * 
* * 
* 

3. Equilateral Triangle

Example of an equilateral triangle:

 Input: 5
Output:
    *
   * *
  * * *
 * * * *
* * * * *

In this example, it can be observed that if there are n rows in the pattern then ith row has i elements and has (n - i) leading spaces.

C++
#include <iostream>
using namespace std;

int main(){
    int n = 5;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < n - i; j++)
            cout << " ";
        for (int j = 1; j <= i; j++)
            cout << "* ";
        cout << endl;
    }
    return 0;
}

Output
    * 
   * * 
  * * * 
 * * * * 
* * * * * 

Explanation:

  • int n = 5, sets the height of the pyramid.
  • The outer loop controls the number of rows.
  • The first inner loop prints leading spaces to center the stars and the second inner loop prints stars in increasing order in each row.

4. Inverted Equilateral Triangle

Example of an inverted equilateral triangle:

Input: 5
Output:
* * * * *
 * * * *
  * * *
   * *
    *

It can be observed if there are n rows in the pattern then ith row has n-i+1 elements and i-1 leading spaces.

C++
#include <iostream>
using namespace std;

int main(){
    int n = 5;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j < i; j++)
            cout << " ";
        for (int j = 1; j <= n - i + 1; j++)
            cout << "* ";
        cout << endl;
    }
    return 0;
}

Output
* * * * * 
 * * * * 
  * * * 
   * * 
    * 

Explanation:

  • int n = 5, defines the height of the pattern.
  • The outer loop controls the number of rows.
  • The first inner loop prints increasing leading spaces in each row and the second inner loop prints decreasing stars in each row.

Another Approach: This program creates an inverted mirrored triangle by adding leading spaces in each row while decreasing the number of stars from n to 1.

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

int main(){
    int n = 5;
    for (int i = n; i >= 1; i--) {
        for (int j = 0; j <= n - i; j++)
            cout << " ";
        for (int j = 1; j <= i; j++)
            cout << "* ";
        cout << endl;
    }
    return 0;
}

Output
 * * * * * 
  * * * * 
   * * * 
    * * 
     * 

5. Inverted Mirrored Right Triangle

Example of inverted mirrored right triangles:

Input: 5
Output:
* * * * *
  * * * *
    * * *
      * *
        *

It can be observed if there are n rows in the pattern then ith row has n-i+1 elements and 2*(i-1) leading spaces.

C++
#include <iostream>
using namespace std;

int main(){
    int n = 5;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j < i; j++)
            cout << "  ";
        for (int j = 1; j <= n - i + 1; j++)
            cout << "* ";
        cout << endl;
    }
    return 0;
}

Output
* * * * * 
  * * * * 
    * * * 
      * * 
        * 

Explanation:

  • The outer loop controls the number of rows.
  • The first inner loop prints increasing spaces in each row to shift the pattern to the right and the second inner loop prints decreasing stars in each row.
Comment