0
0
CProgramBeginner · 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, like for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) printf("*"); printf("\n"); } to print a right triangle pattern.
📋

Examples

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

How to Think About It

To print a right triangle pattern, think of rows increasing from 1 to n. For each row, print stars equal to the row number. Use one loop for rows and a nested loop for stars in each row.
📐

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 the inner loop for each iteration
5
After inner loop ends, print a newline to move to the next row
6
Repeat until all rows are printed
💻

Code

c
#include <stdio.h>

int main() {
    int n;
    printf("Enter number of rows: ");
    scanf("%d", &n);

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}
Output
Enter number of rows: 5 * ** *** **** *****
🔍

Dry Run

Let's trace the program with input 3 to see how it prints the right triangle.

1

Input

User enters n = 3

2

First outer loop iteration (i=1)

Inner loop runs j=1 to 1, prints '*' once, then newline

3

Second outer loop iteration (i=2)

Inner loop runs j=1 to 2, prints '**', then newline

4

Third outer loop iteration (i=3)

Inner loop runs j=1 to 3, prints '***', then newline

5

End

All rows printed, program ends

i (row)Stars printed
1*
2**
3***
💡

Why This Works

Step 1: Outer loop controls rows

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

Step 2: Inner loop prints stars

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

Step 3: Newline after each row

After printing stars for a row, printf("\n") moves the cursor to the next line to start the next row.

🔄

Alternative Approaches

Using while loops
c
#include <stdio.h>

int main() {
    int n, i = 1, j;
    printf("Enter number of rows: ");
    scanf("%d", &n);

    while (i <= n) {
        j = 1;
        while (j <= i) {
            printf("*");
            j++;
        }
        printf("\n");
        i++;
    }
    return 0;
}
This uses <code>while</code> loops instead of <code>for</code> loops; functionally the same but slightly longer code.
Using recursion
c
#include <stdio.h>

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

void printTriangle(int row, int n) {
    if (row > n) return;
    printStars(row);
    printf("\n");
    printTriangle(row + 1, n);
}

int main() {
    int n;
    printf("Enter number of rows: ");
    scanf("%d", &n);
    printTriangle(1, n);
    return 0;
}
This uses recursion to print stars and rows, which is elegant but less efficient and harder for beginners.

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 a fixed amount of extra space for variables and does not allocate additional memory proportional to input size, so O(1).

Which Approach is Fastest?

The nested for loop approach is straightforward and efficient for this problem; recursion adds overhead and is less efficient.

ApproachTimeSpaceBest For
Nested for loopsO(n^2)O(1)Simple and efficient for beginners
Nested while loopsO(n^2)O(1)Similar to for loops, slightly more verbose
RecursionO(n^2)O(n)Elegant but uses more stack space and complex
💡
Use nested loops where the inner loop prints stars up to the current row number.
⚠️
Beginners often forget to print a newline after each row, causing all stars to print on one line.