A reverse descending number triangle prints digits i..1 on each row, with the outer loop counting down so the first row is longest.
Remember
Rule: for i = n..1, print i..1
54321
4321
321
21
1 ← 5 rows
In C# the outer loop runs i = rows..1, the inner loop prints j from i down to 1, then Console.WriteLine() ends the row. Flip the inner loop to j = 1..i for Program 1’s ascending digits on a shrinking triangle.
Approach
How to Solve It
One nested-loop idea with two useful variants — packed digits and spaced digits.
Method
Idea
Best for
Nested descending loops
Outer shrinks length; inner prints i..1
Learning, interviews, exams
Spaced digits
Same loops; print j + " "
Practice variants
Pseudocode
Pseudocode
for i from rows down to 1:
for j from i down to 1:
print j (no newline)
print newline
Cheat sheet
Goal
Pattern
Shrink each row
for (i = rows; i >= 1; i--)
Print digits i..1
for (j = i; j >= 1; j--) Console.Write(j);
End the row
Console.WriteLine();
Add spaces
Console.Write(j + " ");
Program 1 shape instead
for (j = 1; j <= i; j++) Console.Write(j);
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each digit j
Console.WriteLine
Ends the current line
After the inner loop
Try it
Live Preview
Change the row count and the reverse descending triangle updates instantly — including the total digit count.
Whole numbers from 1 to 9 (keeps digits single-width). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 digits
54321
4321
321
21
1
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i and the digits the inner loop prints downward.
i
Inner j
Printed row
Digits
4
4..1
4321
4
3
3..1
321
3
2
2..1
21
2
1
1..1
1
1
Total digits are 4+3+2+1 = 10 — the triangular number n(n+1)/2.
Code
C# Programs
Three complete programs: fixed height, row-count input, and spaced digits. Use View Output for sample results.
Example 1 — Fixed rows = 5
Outer loop shrinks the length; inner loop prints digits i..1.
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 = i; j >= 1; j--)
{
Console.Write(j);
}
Console.WriteLine();
}
}
}
}
Output
54321
4321
321
21
1
How It Works
1. Shrink the count.for (i = rows; i >= 1; i--) yields 5, 4, 3, 2, 1.
2. Print downward. When i = 5, the inner loop prints 54321; when i = 1, just 1.
3. Newline.WriteLine() after the inner loop starts the next shorter row.
Example 2 — User Input
Read the row count at runtime. Prefer int.TryParse in real apps so bad input does not throw.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows;
int i, j;
Console.Write("Enter the number of rows: ");
if (!int.TryParse(Console.ReadLine(), out rows) || rows < 1)
{
Console.WriteLine("Please enter a positive whole number.");
return;
}
for (i = rows; i >= 1; i--)
{
for (j = i; j >= 1; j--)
{
Console.Write(j);
}
Console.WriteLine();
}
}
}
}
2. Same nested loops. Four rows start at 4321 and end at 1.
Example 3 — Spaced Digits
Same reverse-descending shape — only the print statement adds a space.
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 = i; j >= 1; j--)
{
Console.Write(j + " ");
}
Console.WriteLine();
}
}
}
}
Output
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
How It Works
1. Same loops. Outer still shrinks; inner still prints i..1.
2. Add gaps.Write(j + " ") inserts a space after each digit.
Edge Cases & Pitfalls
Check these before calling the solution done.
ascend j
j = 1..i instead of j = i..1
That prints ascending digits (Program 1). Keep for (j = i; j >= 1; j--).
grow outer
i = 1..rows instead of rows..1
That grows the triangle. Use for (i = rows; i >= 1; i--) so the first row is longest.
WriteLine early
WriteLine(j) inside the inner loop
That puts each digit on its own line. Use Write(j); call WriteLine() only after the row finishes.
rows = 1
Single 1
Output is just 1 — a good sanity check.
rows ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
Bad input
Convert.ToInt32 throws
Prefer int.TryParse so non-numeric input does not crash the program.
Analysis
Time and Space Complexity
Program
Time
Extra space
Packed digits (Examples 1–2)
O(n²)
O(1)
Spaced digits (Example 3)
O(n²)
O(1)
There are n rows printing n+(n-1)+…+1 digits, so total work is n(n+1)/2.
Remember
Key Takeaways
Rule: for i = n..1, print digits i..1 with Write.
Count down twice: outer shrinks length; inner reverses the digits.
Write vs WriteLine: digits stay on the line; WriteLine advances after each row.
Next step: Program 4 left-aligns a descending triangle with leading spaces.
One line: for each length from n down to 1, print i..1 with Write, then WriteLine.
Frequently Asked Questions
The inner loop runs j = i down to 1, so each row prints i, i-1, …, 1.
Because rows = 5 and the outer loop starts with i = rows. The first row prints from 5 down to 1.
Program 2 prints i..rows (left-shifted). Program 3 prints i..1 (reverse descending) with a shrinking outer loop.
Keep the descending outer loop but change the inner loop to j = 1..i (ascending) — that is Program 1’s shape.
Replace 5 with rows in the outer bound — see Example 2.
Use Console.Write(j + " ") instead of Console.Write(j) — see Example 3.
O(n²) for n rows. Total prints are n + (n-1) + … + 1 = n(n+1)/2.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
Only one row prints — a single 1.
🤔
Did you know?
This pattern prints each row in descending order from i down to 1. The outer loop shrinks the row length while the inner loop counts downward — producing 54321, 4321, 321, and so on.