Odd-Length Descending Number Triangle in C#

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Step Size Loop

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:

Output
1234567
12345
123
1
1

Complete C# Program

The outer loop decreases by 2. The inner loop prints from 1 to i on each row.

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();
            }
        }
    }
}

🧠 How It Works

1

Start from an odd maximum

The loop begins at i = 7 so the first row prints 7 digits.

Setup
2

Decrease by 2 each row

i -= 2 makes the row lengths 7, 5, 3, 1.

Row length
3

Print 1..i

The inner loop prints digits from 1 up to the current i.

Number printing
=

Odd-length descending triangle

This is a compact way to print only odd row widths using a custom loop step.

2

Variation — Configurable Maximum

Print the same pattern starting from a user-provided odd number:

C#
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

1

i -= 2 is a simple way to visit only odd row widths.

2

The inner loop prints 1..i to form each row.

3

The output rows are 7, 5, 3, 1 for the default maximum of 7.

4

Changing the loop step changes the entire shape.

❓ Frequently Asked Questions

Because the outer loop decrements by 2, so it prints only odd lengths (7, 5, 3, 1) and skips even lengths (6, 4, 2).
Yes. Change the starting value of the outer loop (or read it as input) and keep i -= 2.
O(n²) for maximum width 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.

All Number Patterns →
Did you know?

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.

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