Right-Angled Triangle Star Pattern in C#

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:
*
**
***
****
*****Complete C# Program
Fixed rows = 5 version:
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
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.
Outer loop (rows)
for (i = 1; i <= rows; i++) walks each row from 1 through rows.
Inner loop (stars)
for (j = 1; j <= i; j++) prints exactly i stars using Console.Write("*").
New line
Console.WriteLine() after the inner loop ends the current row so the next outer iteration starts on a fresh line.
Right-angled triangle
Total stars: 1+2+…+n = n(n+1)/2 — O(n²) for n = rows, O(1) extra memory. Long rows scroll horizontally inside the green preview on phones.
Variation — User Input Version
Read the row count with Console.ReadLine() and Convert.ToInt32 (or int.Parse):
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.TryParseonConsole.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.WriteLineinside the inner loop (would break the row) - Omitting
Console.WriteLine()after each row - Using
Convert.ToInt32without handling non-numeric input
Key Takeaways
The outer loop picks the row; the inner loop prints i stars on that row.
Console.Write keeps output on one line; Console.WriteLine() starts the next row.
Time complexity is O(n²) for n rows.
This pattern is the usual starting point before pyramids, diamonds, and hollow shapes.
Read rows from the console when you want a flexible program.
❓ Frequently Asked Questions
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.for loops express that directly.for (i = rows; i >= 1; i--), while keeping j from 1 to i. The first line then has rows stars.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.
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.
12 people found this page helpful
