C++ Program To Print Triangle Pattern

Last Updated : 18 Aug, 2026

Triangle patterns are useful for practicing nested loops and understanding how rows, columns, spaces, and symbols are controlled in C++.

  • Each pattern uses nested loops to control the number of spaces and symbols printed in every row.
  • The number of spaces or stars changes according to the row number to create the required shape.

Right Triangle

For n rows, the ith row contains i stars. The outer loop controls the rows, while the inner loop prints the required number of stars.

Illustration

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

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
* 
* * 
* * * 
* * * * 
* * * * * 

Inverted Right Triangle

For n rows, the ith row contains n - i + 1 stars. The number of stars decreases by one after every row.

Illustration

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

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
* * * * * 
* * * * 
* * * 
* * 
* 

Alternative Implementation

The same pattern can also be generated by starting the outer loop from n and decreasing it to 1.

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

int main()
{
    int n = 5;

    for (int i = n; i >= 1; i--)
    {
        for (int j = 1; j <= i; j++)
        {
            cout << "* ";
        }

        cout << '\n';
    }

    return 0;
} 

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

Equilateral Triangle

An equilateral triangle pattern requires both leading spaces and stars. For the ith row, n - i spaces are printed before i stars.

Illustration

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

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
    * 
   * * 
  * * * 
 * * * * 
* * * * * 

Inverted Equilateral Triangle

In an inverted equilateral triangle, the number of stars decreases from n to 1, while the number of leading spaces increases with each row.

For the ith row:

  • i - 1 leading spaces are printed.
  • n - i + 1 stars are printed.

Illustration

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

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
* * * * * 
 * * * * 
  * * * 
   * * 
    * 

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 <iostream>
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 << '\n';
    }

    return 0;
} 

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

Inverted Mirrored Right Triangle

In this pattern, the number of stars decreases from n to 1, while the pattern moves farther to the right in every row.

For the ith row:

  • i - 1 groups of leading spaces are printed.
  • n - i + 1 stars are printed.

Illustration

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

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
* * * * * 
  * * * * 
    * * * 
      * * 
        * 
Comment