Right-Aligned Right-Angled Triangle Star Pattern in C#

What You'll Learn
How to print a right-aligned right-angled triangle in C#. Each row still has 1, 2, …, rows stars like Program 1, but you print (rows - i) spaces first so the stars shift right and the bottom row lines up on the right.
With rows = 5, the first line has four spaces and one star; the last line has five stars and no leading spaces.
⭐ Pattern Output
When you run the program with rows = 5, you’ll see this output:
*
**
***
****
*****Complete C# Program
Fixed rows = 5 version (spaces then stars):
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
int i, j, k;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= rows - i; j++)
{
Console.Write(" ");
}
for (k = 1; k <= i; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
}🧠 How It Works
Setup
rows is the height. i indexes the row; j runs the space loop; k runs the star loop. Braces group each for body under Main.
Outer loop (rows)
for (i = 1; i <= rows; i++) walks from one star on line 1 up to rows stars on the last line.
Padding: Console.Write(" ")
for (j = 1; j <= rows - i; j++) { Console.Write(" "); } prints rows - i spaces so the star block shifts right while the right edge stays straight.
Stars: Console.Write("*")
for (k = 1; k <= i; k++) { Console.Write("*"); } prints exactly i asterisks after the padding—same star count per row as a left-aligned triangle, different horizontal offset.
New line
Console.WriteLine() ends the row (blank overload adds only a newline). Every row has (rows - i) + i = rows characters before the break.
Right-aligned triangle
Star total n(n+1)/2; O(n²) output for n = rows, O(1) extra space. Full-width lines scroll horizontally in the green preview on narrow viewports.
Variation — User Input Version
Read rows with Console.ReadLine() and Convert.ToInt32:
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows;
int i, j, k;
Console.Write("Enter the number of rows: ");
rows = Convert.ToInt32(Console.ReadLine());
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= rows - i; j++)
{
Console.Write(" ");
}
for (k = 1; k <= i; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Validate
rows > 0after parsing input - Remove the space loop to recover Program 1
- Use
int.TryParseinstead ofConvert.ToInt32for safer console apps - Print digits instead of
*for a right-aligned number triangle
Avoid
- Swapping
rows - iandibetween the two inner loops - Off-by-one on
j <= rows - i(use<=, not<, for this formulation) - Mixing tabs with spaces (alignment breaks across fonts)
Key Takeaways
Print (rows - i) spaces, then i stars, each row.
Two inner loops handle two different per-row counts (padding vs stars).
Star counts match Program 1; only leading padding changes.
Time complexity O(n²) for n rows.
Typical stepping stone toward centered pyramids and diamonds.
❓ Frequently Asked Questions
(rows - i) spaces, then i stars. The spaces push the star run to the right so the longest line is flush on the right.i increases.i stars appear shifted right.n = rows: each row prints Θ(n) characters.Explore More C# Star Patterns!
Mirrored rows, pyramids, and diamonds all build on spaces plus stars.
Right-aligned and left-aligned triangles use the same star counts per row; only leading spaces change. Many courses teach Program 1 first, then add the space loop for this pattern.
12 people found this page helpful
