Right-Angled Triangle Star Pattern in C#

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You'll Learn

How to print a right-angled triangle star pattern in C# using nested for loops, Console.Write for stars on one line, and Console.WriteLine() to move to the next row.

The pattern increases by one star per row: row 1 prints *, row 2 prints **, and so on up to the chosen number of rows.

⭐ Pattern Output

When you run the program with rows = 5, you’ll see this output:

Output
*
**
***
****
*****
1

Complete C# Program

Fixed rows = 5 version:

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows = 5;
            int i, j;

            for (i = 1; i <= rows; i++)
            {
                for (j = 1; j <= i; j++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Entry point and variables

using System; brings in Console. Inside static void Main, int rows = 5; fixes the demo height; i and j index rows and columns.

Setup
2

Outer loop (rows)

for (i = 1; i <= rows; i++) walks each row from 1 through rows.

Row control
3

Inner loop (stars)

for (j = 1; j <= i; j++) prints exactly i stars using Console.Write("*").

Star printing
4

New line

Console.WriteLine() after the inner loop ends the current row so the next outer iteration starts on a fresh line.

Line break
=

Right-angled triangle

Total stars: 1+2+…+n = n(n+1)/2O(n²) for n = rows, O(1) extra memory. Long rows scroll horizontally inside the green preview on phones.

2

Variation — User Input Version

Read the row count with Console.ReadLine() and Convert.ToInt32 (or int.Parse):

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows;
            int i, j;

            Console.Write("Enter the number of rows: ");
            rows = Convert.ToInt32(Console.ReadLine());

            for (i = 1; i <= rows; i++)
            {
                for (j = 1; j <= i; j++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Use int.TryParse on Console.ReadLine() to avoid crashes on bad input
  • Invert the outer loop for an upside-down triangle
  • Replace * with digits or other characters
  • Print a hollow triangle (border stars only)

Avoid

  • Calling Console.WriteLine inside the inner loop (would break the row)
  • Omitting Console.WriteLine() after each row
  • Using Convert.ToInt32 without handling non-numeric input

Key Takeaways

1

The outer loop picks the row; the inner loop prints i stars on that row.

2

Console.Write keeps output on one line; Console.WriteLine() starts the next row.

3

Time complexity is O(n²) for n rows.

4

This pattern is the usual starting point before pyramids, diamonds, and hollow shapes.

5

Read rows from the console when you want a flexible program.

❓ Frequently Asked Questions

For each i from 1 to rows, the inner loop runs j from 1 to i and prints * each time. That yields 1, 2, 3, …, rows stars per line.
You need one loop for which row you are on and another for how many characters belong on that row. Nested for loops express that directly.
Run the outer loop backward, e.g. for (i = rows; i >= 1; i--), while keeping j from 1 to i. The first line then has rows stars.
O(n²) for n = rows, because the total number of Console.Write calls is n(n+1)/2.

Explore More C# Star Patterns!

Continue the series for pyramids, diamonds, and hollow shapes—all built from nested loops.

All C# Star Patterns →
Did you know?

The right-angled triangle is the standard first pattern in many C# and Java console exercises because it teaches loop bounds and output sequencing without extra geometry.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful