Odd-Length Descending Number Triangle in C#

What You’ll Learn
How to print an odd-length descending number triangle in C#. The first row prints 1234567, then 12345, then 123, and finally 1.
The key idea is to reduce the row length by 2 each time using i -= 2.
⭐ Pattern Output
For max = 7, the pattern looks like this:
1234567
12345
123
1Complete C# Program
The outer loop decreases by 2. The inner loop prints from 1 to i on each row.
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();
}
}
}
}🧠 How It Works
Start from an odd maximum
The loop begins at i = 7 so the first row prints 7 digits.
Decrease by 2 each row
i -= 2 makes the row lengths 7, 5, 3, 1.
Print 1..i
The inner loop prints digits from 1 up to the current i.
Odd-length descending triangle
This is a compact way to print only odd row widths using a custom loop step.
Variation — Configurable Maximum
Print the same pattern starting from a user-provided odd number:
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter an odd maximum (e.g., 9): ");
int max = Convert.ToInt32(Console.ReadLine());
if (max % 2 == 0) max -= 1;
for (int i = max; i >= 1; i -= 2)
{
for (int j = 1; j <= i; j++)
{
Console.Write(j);
}
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Print only even widths by starting at an even number and using
i -= 2 - Right-align the triangle by printing leading spaces before each row
- Add spaces between digits with
Console.Write(j + " ")
Avoid
- Using an even start value if you expect the first row to be odd-length
- Forgetting to print a newline after each inner loop
Key Takeaways
i -= 2 is a simple way to visit only odd row widths.
The inner loop prints 1..i to form each row.
The output rows are 7, 5, 3, 1 for the default maximum of 7.
Changing the loop step changes the entire shape.
❓ Frequently Asked Questions
i -= 2.n, since you still print on the order of \(n^2\) characters overall.Explore More C# Number Patterns!
Small tweaks like changing the loop step can create entirely new patterns.
The sum of odd numbers \(1+3+5+\dots+(2k-1)\) equals \(k^2\). That’s why printing only odd-width rows still grows quadratically.
12 people found this page helpful
