Increasing Odd-Length Number Rows in C#

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

What You’ll Learn

How to print a number pattern in C# where each row prints 1..i and the row length increases by 2 each time.

⭐ Pattern Output

For max = 9, the pattern looks like this:

Output
1
123
12345
1234567
123456789
1

Complete C# Program

The outer loop uses i += 2 to build rows of length 1, 3, 5, 7, 9. The inner loop prints digits from 1 to i.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j;

            for (i = 1; i <= 9; i += 2)
            {
                for (j = 1; j <= i; j++)
                    Console.Write(j);
                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Increase by 2

The outer loop increments by 2, so row lengths are odd: 1, 3, 5, 7, 9.

Step size
2

Print 1..i

The inner loop prints numbers from 1 to the current row length i.

Inner loop
=

Odd-length growth

A simple step size change creates a new family of patterns.

2

Variation — User Input Maximum

Read the maximum value (make it odd if needed) and print the same pattern:

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the maximum value: ");
            int max = Convert.ToInt32(Console.ReadLine());

            if (max % 2 == 0) max -= 1;

            for (int i = 1; i <= max; i += 2)
            {
                for (int j = 1; j <= i; j++)
                    Console.Write(j);
                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Add spaces between digits (Console.Write(j + " "))
  • Print in reverse (from i down to 1) for a different look
  • Use the same idea to print odd-only numbers

Avoid

  • Using an even maximum if you want perfectly odd-length steps
  • Forgetting Console.WriteLine() after each row

Key Takeaways

1

i += 2 creates odd row lengths.

2

The inner loop prints a simple sequence from 1 to i.

3

You can parameterize the maximum value easily.

4

Total work grows roughly like \(O(n^2)\).

❓ Frequently Asked Questions

Because the outer loop condition is i <= 9. Change 9 to any odd number to extend it.
Yes. Use for (j = 1; j <= i; j += 2) and print j.
O(n²) for maximum length n since total prints are proportional to 1+3+5+...+n.

Explore More C# Number Patterns!

Try changing the step size to generate even-length or custom-length patterns.

All Number Patterns →
Did you know?

The sequence of odd numbers 1, 3, 5, 7, 9 controls how many items are printed per row in many pyramid-style patterns.

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