C++ Program to Print Triangle Pattern

26/04/2024 0 By indiafreenotes

To create a triangle pattern in C++, you can use nested loops to manipulate the placement of characters (like asterisks *) on each line. The type of triangle you wish to print can vary in shape and size; examples include right-angled triangles, equilateral triangles, or inverted triangles.

Here, I’ll provide a simple C++ program to print a right-angled triangle using asterisks (*). This triangle aligns along the left side, making it straightforward to understand and implement.

Program to Print a Right-Angled Triangle Pattern

#include <iostream>

using namespace std;

int main() {

    int rows;

    cout << “Enter the number of rows for the triangle: “;

    cin >> rows;

    // Loop through each row

    for(int i = 1; i <= rows; i++) {

        // Print stars in each column

        for(int j = 1; j <= i; j++) {

            cout << “* “;


        // Move to the next line after each row is printed

        cout << endl;


    return 0;



Breakdown of the Program

  1. Include Header and Namespace:

The program begins by including the <iostream> header for input and output operations and uses the std namespace.

  1. Input Number of Rows:

The user is prompted to enter the number of rows for the triangle, which determines its height.

  1. Outer Loop:

This loop iterates through each row, from 1 to rows. Each iteration corresponds to a row in the triangle.

  1. Inner Loop:

Inside the outer loop, another loop runs from 1 to the current row number (i). This ensures that the number of asterisks printed increases by one with each new row, forming the right-angled triangle shape.

  1. Printing New Line:

After each row is printed, a newline character is added (cout << endl;) to move to the next row.

Running the Program

When you run this program, it might look something like this if you input 5 for the number of rows:


* *

* * *

* * * *

* * * * *

Each row increases the number of stars printed, and the pattern visually forms a right-angled triangle aligned to the left. This example can be modified to create other types of triangle patterns by adjusting the inner loop and possibly adding additional loops for spaces, depending on whether you want the triangle to be centered or inverted.