0
0
CppProgramBeginner · 2 min read

C++ Program to Print Right Triangle Pattern

Use nested for loops in C++ where the outer loop controls rows and the inner loop prints stars; for example, for(int i=1; i<=n; i++){ for(int j=1; j<=i; j++) cout << "*"; cout << endl; } prints a right triangle pattern.
📋

Examples

Input3
Output* ** ***
Input5
Output* ** *** **** *****
Input1
Output*
🧠

How to Think About It

To print a right triangle pattern, think of each row as having a number of stars equal to its row number. Use one loop to go through each row from 1 to n, and inside it, use another loop to print stars equal to the current row number.
📐

Algorithm

1
Get the number of rows (n) from the user.
2
Start an outer loop from 1 to n for each row.
3
Inside the outer loop, start an inner loop from 1 to the current row number.
4
Print a star (*) in each iteration of the inner loop.
5
After the inner loop ends, print a newline to move to the next row.
6
Repeat until all rows are printed.
💻

Code

cpp
#include <iostream>
using namespace std;

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

Dry Run

Let's trace input 5 through the code to see how the right triangle pattern is printed.

1

Input

User inputs n = 5

2

Outer loop iteration 1

i = 1; inner loop runs j = 1; prints '*' once; prints newline

3

Outer loop iteration 2

i = 2; inner loop runs j = 1 to 2; prints '**'; prints newline

4

Outer loop iteration 3

i = 3; inner loop runs j = 1 to 3; prints '***'; prints newline

5

Outer loop iteration 4

i = 4; inner loop runs j = 1 to 4; prints '****'; prints newline

6

Outer loop iteration 5

i = 5; inner loop runs j = 1 to 5; prints '*****'; prints newline

Row (i)Stars Printed
1*
2**
3***
4****
5*****
💡

Why This Works

Step 1: Outer loop controls rows

The outer for loop runs from 1 to n, representing each row of the triangle.

Step 2: Inner loop prints stars

The inner for loop runs from 1 to the current row number, printing that many stars.

Step 3: Newline after each row

After printing stars for a row, cout << endl; moves the cursor to the next line for the next row.

🔄

Alternative Approaches

Using while loops
cpp
#include <iostream>
using namespace std;

int main() {
    int n, i = 1;
    cin >> n;
    while (i <= n) {
        int j = 1;
        while (j <= i) {
            cout << "*";
            j++;
        }
        cout << endl;
        i++;
    }
    return 0;
}
This uses <code>while</code> loops instead of <code>for</code> loops; it is equally readable but slightly longer.
Using recursion
cpp
#include <iostream>
using namespace std;

void printStars(int count) {
    if (count == 0) return;
    cout << "*";
    printStars(count - 1);
}

void printTriangle(int row, int n) {
    if (row > n) return;
    printStars(row);
    cout << endl;
    printTriangle(row + 1, n);
}

int main() {
    int n;
    cin >> n;
    printTriangle(1, n);
    return 0;
}
This recursive approach is elegant but less intuitive for beginners and uses more stack space.

Complexity: O(n^2) time, O(1) space

Time Complexity

The outer loop runs n times and the inner loop runs up to n times in the last iteration, resulting in roughly n*(n+1)/2 operations, which is O(n^2).

Space Complexity

The program uses constant extra space for loop variables and output, so space complexity is O(1).

Which Approach is Fastest?

All approaches have similar time complexity; iterative for loops are simplest and most efficient in practice.

ApproachTimeSpaceBest For
Nested for loopsO(n^2)O(1)Simplicity and readability
Nested while loopsO(n^2)O(1)Alternative loop style
RecursionO(n^2)O(n)Learning recursion, elegant code
💡
Use nested loops where the inner loop count depends on the outer loop index to print patterns.
⚠️
A common mistake is printing all stars in one line without moving to the next line after each row.