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

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Spaces + decreasing stars

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:

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, 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

1

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.

Setup
2

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.

Row control
3

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.

Indent
4

Stars: Console.Write("*")

for (k = i; k <= rows; k++) { Console.Write("*"); } prints rows - i + 1 stars (equivalently for (k = rows; k >= i; k--)).

Stars
5

New line

Console.WriteLine() ends the row. Characters before the newline: (i - 1) + (rows - i + 1) = rows every time.

Line break
=

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.

2

Variation — User Input Version

Read rows with Console.ReadLine() and Convert.ToInt32:

C#
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 > 0 after parsing
  • Rewrite stars as for (k = 1; k <= rows - i + 1; k++) and compare readability
  • Change the star loop to 1..i to recover Program 3
  • Use int.TryParse for safer console input

Avoid

  • Using j <= i instead of j < i for the space loop (adds an extra space)
  • Using k from 1 to i here—that is Program 3, not this pattern
  • Mixing tabs with spaces in output

Key Takeaways

1

Row i: i - 1 spaces, then rows - i + 1 stars.

2

for (k = i; k <= rows; k++) matches that star count in one loop.

3

Program 3 grows stars per row; Program 4 shrinks them—same right column.

4

Time complexity O(n²) for n rows.

5

Together with Programs 1–3 you cover left, inverted, right-aligned, and inverted right-aligned triangles.

❓ Frequently Asked Questions

Print 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.
That interval has length 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.
Program 3: (rows - i) spaces, stars 1..i. Program 4: (i - 1) spaces, stars i..rows. Same alignment style; inverted star progression.
O(n²) for 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.

All C# Star Patterns →
Did you know?

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.

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