Inverted Right-Angled Triangle Star Pattern in C#

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:
*****
****
***
**
*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 = rows; i >= 1; i--)
{
for (j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
}🧠 How It Works
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.
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.
Inner loop (stars)
for (j = 1; j <= i; j++) prints i stars with Console.Write("*").
New line
Console.WriteLine() ends the current row before i changes.
Inverted triangle
Decreasing i prints the wide line first. Total stars still n(n+1)/2 — O(n²) time, O(1) extra space. The widest line uses the same scrollable glyph area on small screens.
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;
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.TryParseinstead ofConvert.ToInt32for safer console apps - Keep
iincreasing 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
5in the outer loop when you already have arowsvariable - Confusing “first line” with “row index 1”—here the first line uses
i == rows
Key Takeaways
The outer loop runs rows → 1; the inner loop still runs 1 → i.
When i == k, that output line has exactly k stars; the first line is the widest.
Time complexity stays O(n²) for n rows.
Forward outer loop plus (rows - i + 1) inner bound is an equivalent formulation.
Pair this with Program 1 to see how loop direction changes the picture.
❓ Frequently Asked Questions
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.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.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).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.
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.
12 people found this page helpful
