C# Descending Number Triangle Pattern (Odd Length)
Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
An odd-length descending number triangle prints digits 1..i on each row, but the outer loop steps by 2 so only odd widths appear.
Remember
Rule: for i = max..1 step −2, print 1..i
1234567
12345
123
1 ← max = 7 (odd widths only)
In C# you solve it with for (i = max; i >= 1; i -= 2) and an inner loop that prints 1..i with Console.Write, then WriteLine() ends the row. Change the step to i-- if you want every width (Program 1 style).
Approach
How to Solve It
Start with the odd-only step of 2, then compare briefly with a full step-by-1 triangle.
Method
Idea
Best for
Step by 2
Outer visits odd widths only; inner prints 1..i
Learning loop steps, this pattern
Step by 1
Same inner loop; outer uses i-- for every width
Contrast / full descending triangle
Pseudocode
Pseudocode
for i from max down to 1 step −2:
for j from 1 to i:
print j (no newline)
print newline
Cheat sheet
Goal
Pattern
Odd widths only
for (i = max; i >= 1; i -= 2)
Print digits 1..i
for (j = 1; j <= i; j++) Console.Write(j);
End the row
Console.WriteLine();
Force odd max
if (max % 2 == 0) max -= 1;
Every width
for (i = max; i >= 1; i--)
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 maximum width and the odd-length triangle updates instantly. Even values are adjusted down to the nearest odd number.
Whole numbers from 1 to 9. Even input becomes the next lower odd max (e.g. 6 → 5).
Live resultmax 7 · 16 digits
1234567
12345
123
1
Trace
Worked Walkthrough — max = 7
Trace each outer-loop value of i (stepping by 2) and the ascending digits printed.
i
Inner j
Printed row
Digits
7
1..7
1234567
7
5
1..5
12345
5
3
1..3
123
3
1
1..1
1
1
Total digits are 7+5+3+1 = 16 — the square ((max+1)/2)² for odd max.
Code
C# Programs
Three complete programs: fixed odd max, input with odd forcing, and a step-by-1 contrast. Use View Output for sample results.
Example 1 — Fixed max = 7
Outer loop steps by 2 for odd widths; inner loop prints digits 1..i.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j;
for (i = 7; i >= 1; i -= 2)
{
for (j = 1; j <= i; j++)
{
Console.Write(j);
}
Console.WriteLine();
}
}
}
}
Output
1234567
12345
123
1
How It Works
1. Step by 2.for (i = 7; i >= 1; i -= 2) yields 7, 5, 3, 1 — even widths never run.
2. Print digits. When i = 7, the inner loop prints 1234567; when i = 1, just 1.
3. Newline.WriteLine() after the inner loop starts the next shorter odd-length row.
Example 2 — User Input
Read the maximum at runtime; force it odd if the user enters an even number.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int max;
int i, j;
Console.Write("Enter an odd maximum (e.g., 9): ");
if (!int.TryParse(Console.ReadLine(), out max) || max < 1)
{
Console.WriteLine("Please enter a positive whole number.");
return;
}
if (max % 2 == 0) max -= 1;
for (i = max; i >= 1; i -= 2)
{
for (j = 1; j <= i; j++)
{
Console.Write(j);
}
Console.WriteLine();
}
}
}
}
Output (when user enters 6)
12345
123
1
How It Works
1. Validate and force odd.TryParse rejects bad input; if (max % 2 == 0) max -= 1 turns 6 into 5.
2. Same nested loops. With max = 5, rows are 12345, 123, and 1.
Example 3 — Step by 1 (Contrast)
Change i -= 2 to i-- to print every width — a full descending triangle.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int max = 5;
for (int i = max; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
Console.Write(j);
}
Console.WriteLine();
}
}
}
}
Output
12345
1234
123
12
1
How It Works
1. Every width.i-- visits 5, 4, 3, 2, 1 — including even lengths that Example 1 skips.
2. Same inner loop. Digits are still 1..i; only the outer step changed. Keep i -= 2 for the odd-only lesson.
Edge Cases & Pitfalls
Check these before calling the solution done.
wrong step
i-- instead of i -= 2
That prints every width (Example 3). Keep stepping by 2 for odd-only rows.
even max
Starting at an even maximum
With max = 6 and no adjustment, the first row is even-length. Force odd with max -= 1 when needed.
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.
max = 1
Single digit
Output is just 1 — a good sanity check.
max ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
multi-digit
max > 9
Write(10) prints two characters, so columns drift. Labs usually stick to odd max values up to 9.
Analysis
Time and Space Complexity
Program
Time
Extra space
Odd-only loops (Examples 1–2)
O(n²)
O(1)
Step by 1 (Example 3)
O(n²)
O(1)
Odd widths print 1+3+…+n = ((n+1)/2)² digits for odd n — still quadratic in the maximum width.
Remember
Key Takeaways
Rule: for odd i from max down to 1 stepping by 2, print 1..i.
Step matters:i -= 2 skips even widths; i-- prints every width.
Write vs WriteLine: digits stay on the line; WriteLine advances after each row.
Next step: Program 15 builds an alternating binary number triangle.
One line: for each odd width from max down to 1, print 1..i with Write, then WriteLine.
Frequently Asked Questions
The outer loop uses i -= 2, so it visits only odd widths: 7, 5, 3, 1. Even lengths like 6, 4, 2 are skipped.
Because i -= 2 skips even row lengths. After 1234567 (7 digits), the next row is 5 digits (12345), not 6.
Console.Write stays on the same line. Console.WriteLine ends the current line. Digits use Write(j); the row break uses WriteLine after the inner loop.
Change the outer loop step to i-- (or i -= 1). Then you print 7, 6, 5, 4, 3, 2, 1 — every width.
Program 13 alternates ascending/descending direction with i % 2 (12345, 4321, 123, 21, 1). Program 14 always prints 1..i but only for odd row lengths using i -= 2.
O(n²) where n is the maximum row width. You print about 1+3+5+…+n odd-width digits, which is still O(n²).
Subtract 1 to make it odd (e.g. if max % 2 == 0 then max -= 1) so the first row stays odd-length, or document that even input shifts the pattern.
Prefer int.TryParse(Console.ReadLine(), out max) so bad input does not throw FormatException.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
Only odd-length rows print. The outer loop uses i −= 2 (7, 5, 3, 1) and the inner loop prints 1..i — still O(n²) total digit prints for maximum width n.