0
0
CsharpProgramBeginner · 2 min read

C# Program to Print Right Triangle Pattern

Use nested for loops in C# where the outer loop controls the rows and the inner loop prints stars * up to the current row number, like for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { Console.Write("*"); } Console.WriteLine(); }.
📋

Examples

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

How to Think About It

To print a right triangle pattern, think of each line as having a number of stars equal to the line number. Start from line 1 with one star, then line 2 with two stars, and so on until the last line. Use one loop to go through each line and another loop inside it to print the stars for that line.
📐

Algorithm

1
Get the number of rows (n) from the user or set it.
2
Start a loop from 1 to n for each row.
3
Inside this loop, start another loop from 1 to the current row number.
4
Print a star (*) in the inner loop without moving to the next line.
5
After the inner loop ends, move to the next line.
6
Repeat until all rows are printed.
💻

Code

csharp
using System;
class Program {
    static void Main() {
        int n = 5;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                Console.Write("*");
            }
            Console.WriteLine();
        }
    }
}
Output
* ** *** **** *****
🔍

Dry Run

Let's trace the example with n = 3 through the code.

1

Start outer loop with i = 1

Inner loop runs j from 1 to 1, prints '*' once, then moves to next line.

2

Outer loop i = 2

Inner loop runs j from 1 to 2, prints '**', then moves to next line.

3

Outer loop i = 3

Inner loop runs j from 1 to 3, prints '***', then moves to next line.

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 a new row.

Step 2: Inner loop prints stars

The inner for loop prints stars equal to the current row number, creating the triangle shape.

Step 3: Move to next line after each row

After printing stars for a row, Console.WriteLine() moves the cursor to the next line for the next row.

🔄

Alternative Approaches

Using while loops
csharp
using System;
class Program {
    static void Main() {
        int n = 5;
        int i = 1;
        while (i <= n) {
            int j = 1;
            while (j <= i) {
                Console.Write("*");
                j++;
            }
            Console.WriteLine();
            i++;
        }
    }
}
This uses while loops instead of for loops; it works the same but is less concise.
Using string constructor
csharp
using System;
class Program {
    static void Main() {
        int n = 5;
        for (int i = 1; i <= n; i++) {
            Console.WriteLine(new string('*', i));
        }
    }
}
This uses the string constructor to create a line of stars, making the code shorter and easier to read.

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 regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

Using the string constructor method is generally faster and cleaner than nested loops because it avoids manual character printing.

ApproachTimeSpaceBest For
Nested for loopsO(n^2)O(1)Simple and clear for beginners
While loopsO(n^2)O(1)Alternative loop style, less concise
String constructorO(n^2)O(1)Cleaner code and faster execution
💡
Use nested loops where the inner loop prints stars up to the current row number to form the triangle.
⚠️
Beginners often forget to move to the next line after printing stars for each row, causing all stars to print on one line.