Inverted Right-Angled Triangle Star Pattern in C#

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Reverse iteration

What You'll Learn

How to print an inverted right-angled triangle in C#: keep the same inner loop as Program 1 (j from 1 to i), but run the outer loop from rows down to 1 so the first line is the widest.

With rows = 5, you get five stars, then four, then three, and so on.

⭐ 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 = rows; i >= 1; i--)
            {
                for (j = 1; j <= i; j++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Entry point and variables

using System; exposes Console. In Main, int rows = 5; fixes the demo; the first printed line has rows stars. i and j drive the outer and inner loops.

Setup
2

Outer loop (rows, reverse)

for (i = rows; i >= 1; i--) starts at the widest row and counts down. Each value of i is how many stars that line prints.

Row control
3

Inner loop (stars)

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

Star printing
4

New line

Console.WriteLine() ends the current row before i changes.

Line break
=

Inverted triangle

Decreasing i prints the wide line first. Total stars still n(n+1)/2O(n²) time, O(1) extra space. The widest line uses the same scrollable glyph area on small screens.

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;

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

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

💡 Tips for Enhancement

Try These

  • Parse input with int.TryParse instead of Convert.ToInt32 for safer console apps
  • Keep i increasing and print (rows - i + 1) stars for the same picture
  • Compare with Program 1 line by line
  • Print digits instead of * for a number triangle

Avoid

  • Omitting Console.WriteLine() after each row
  • Hard-coding 5 in the outer loop when you already have a rows variable
  • Confusing “first line” with “row index 1”—here the first line uses i == rows

Key Takeaways

1

The outer loop runs rows1; the inner loop still runs 1i.

2

When i == k, that output line has exactly k stars; the first line is the widest.

3

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

4

Forward outer loop plus (rows - i + 1) inner bound is an equivalent formulation.

5

Pair this with Program 1 to see how loop direction changes the picture.

❓ Frequently Asked Questions

The outer loop starts at i = rows and ends at i = 1. Each line prints i stars, so the first line is longest and each next line is shorter by one.
Program 1 uses for (i = 1; i <= rows; i++) so stars grow. This program uses for (i = rows; i >= 1; i--) so stars shrink. The inner j loop is the same idea.
Yes. Use for (i = 1; i <= rows; i++) and change the inner bound to j <= rows - i + 1 (or use a separate variable for the star count).
O(n²) for n = rows. The total number of Console.Write calls is still n(n+1)/2.

Explore More C# Star Patterns!

Build on Programs 1 and 2 with mirrored shapes, pyramids, and more nested-loop practice.

All C# Star Patterns →
Did you know?

The inverted triangle uses the same inner loop as Program 1; only the outer loop direction changes. Learning both in sequence is one of the quickest ways to read nested loops fluently.

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