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

What You'll Learn
How to print an inverted right-aligned triangle in C#: same right edge as Program 3, but each row has fewer stars (5, 4, 3, 2, 1 when rows = 5). Use (i - 1) leading spaces and a star loop with k from i to rows (i.e. rows - i + 1 stars).
This pairs with Program 3 the same way Program 2 pairs with Program 1—same alignment, opposite vertical direction for star counts.
⭐ Pattern Output
When you run the program with rows = 5, you’ll see:
*****
****
***
**
*Complete C# Program
Fixed rows = 5 version:
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 < i; j++)
{
Console.Write(" ");
}
for (k = i; k <= rows; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
}🧠 How It Works
Setup
rows sets the first line width. i is the row index (1 to rows); j prints leading spaces; k prints stars. Nested loops live under Main.
Outer loop (rows)
for (i = 1; i <= rows; i++) walks from the widest row (i == 1) to one star (i == rows), inverted but still flush on the right.
Indent: Console.Write(" ")
for (j = 1; j < i; j++) { Console.Write(" "); } prints i - 1 spaces—none on row 1, more on each lower row so the shrinking star block stays right-aligned.
Stars: Console.Write("*")
for (k = i; k <= rows; k++) { Console.Write("*"); } prints rows - i + 1 stars (equivalently for (k = rows; k >= i; k--)).
New line
Console.WriteLine() ends the row. Characters before the newline: (i - 1) + (rows - i + 1) = rows every time.
Inverted, still right-aligned
Star total n(n+1)/2; O(n²) output for n = rows, O(1) extra space. Wide rows scroll horizontally inside the green preview on phones.
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 < i; j++)
{
Console.Write(" ");
}
for (k = i; k <= rows; k++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Validate
rows > 0after parsing - Rewrite stars as
for (k = 1; k <= rows - i + 1; k++)and compare readability - Change the star loop to
1..ito recover Program 3 - Use
int.TryParsefor safer console input
Avoid
- Using
j <= iinstead ofj < ifor the space loop (adds an extra space) - Using
kfrom1toihere—that is Program 3, not this pattern - Mixing tabs with spaces in output
Key Takeaways
Row i: i - 1 spaces, then rows - i + 1 stars.
for (k = i; k <= rows; k++) matches that star count in one loop.
Program 3 grows stars per row; Program 4 shrinks them—same right column.
Time complexity O(n²) for n rows.
Together with Programs 1–3 you cover left, inverted, right-aligned, and inverted right-aligned triangles.
❓ Frequently Asked Questions
i - 1 spaces, then let k run from i to rows to print stars. The star block gets shorter on the left while the right edge stays fixed.rows - i + 1, which is the number of stars on row i. Equivalent: k = 1..(rows - i + 1) or k from rows down to i.(rows - i) spaces, stars 1..i. Program 4: (i - 1) spaces, stars i..rows. Same alignment style; inverted star progression.n = rows: each row prints Θ(n) characters.Explore More C# Star Patterns!
Centered pyramids and diamonds build on the same space-and-star loop ideas.
Program 4 is Program 3 with the star-count rule flipped: widest row on top instead of bottom. If you understand how i maps to spaces and star length in one, you can read the other quickly.
12 people found this page helpful
