C# Number Rows Pattern (Odd Length)

Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

An increasing odd-length number rows pattern prints digits 1..i on each row, where i walks only odd values — so lengths grow 1, 3, 5, 7, 9.

Remember
Rule: for i = 1, 3, 5… print 1..i (no spaces)

1
123
12345
1234567
123456789   ← max = 9

In C# you use for (i = 1; i <= max; i += 2) in the outer loop and Console.Write(j) in the inner loop, then WriteLine() ends each row. Flip the inner step to j += 2 to print only odd digits instead.

How to Solve It

One nested-loop idea with two useful variants — consecutive digits and odd-only digits.

MethodIdeaBest for
Odd row lengthsOuter i += 2; inner prints 1..i with WriteLearning, interviews, exams
Odd digits onlySame outer loop; inner also uses j += 2Practice variants

Pseudocode

Pseudocode
for i from 1 to max step 2:
    for j from 1 to i:
        print j (no newline, no space)
    print newline

Cheat sheet

GoalPattern
Odd row lengthsfor (i = 1; i <= max; i += 2)
Print digits 1..ifor (j = 1; j <= i; j++) Console.Write(j);
End the rowConsole.WriteLine();
Odd digits onlyfor (j = 1; j <= i; j += 2) Console.Write(j);
Even max from userif (max % 2 == 0) max -= 1;

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach digit j (no space)
Console.WriteLineEnds the current lineAfter the inner loop

Live Preview

Change the maximum odd length and the pattern updates instantly — including the total digit count.

Odd whole numbers from 1 to 9 (keeps digits single-width). Even values are adjusted down by 1.

Live result max 9 · 25 digits
1
123
12345
1234567
123456789

Worked Walkthrough — max = 7

Trace each odd outer-loop value of i and the digits the inner loop prints.

iInner jPrinted rowDigits
11..111
31..31233
51..5123455
71..712345677

Total digits are 1+3+5+7 = 16 — the sum of the first few odd numbers.

C# Programs

Three complete programs: fixed maximum, user-input maximum, and an odd-digits-only variant. Use View Output for sample results.

Example 1 — Fixed max = 9

Outer loop steps by 2; inner loop concatenates digits 1..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. Odd lengths. i += 2 yields 1, 3, 5, 7, 9 as row widths.

2. Concatenate digits. When i = 5, the inner loop prints 12345 with no spaces.

3. Newline. WriteLine() after the inner loop starts the next longer odd row.

Example 2 — User Input

Read the maximum at runtime. If the user enters an even value, subtract 1 so the last row stays odd.

C#
using System;

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

            Console.Write("Enter the maximum value: ");
            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 = 1; i <= max; i += 2)
            {
                for (j = 1; j <= i; j++)
                {
                    Console.Write(j);
                }
                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Validate max. TryParse rejects non-numeric input; require max >= 1.

2. Force odd. Even input becomes max - 1, then the same nested loops print the rows.

Example 3 — Odd Digits Only j += 2

Keep odd row lengths, but print only odd digits on each row.

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 += 2)
                {
                    Console.Write(j);
                }
                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Same outer loop. Row lengths are still 1, 3, 5, 7, 9.

2. Skip even digits. j += 2 prints only 1, 3, 5, … up to i.

Edge Cases & Pitfalls

Check these before calling the solution done.

i++

i++ instead of i += 2

That prints every length (1, 12, 123…). Keep i += 2 for odd row lengths only.

spaces

Write(j + " ")

That inserts gaps. Use Write(j) for concatenated digits like 12345.

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 1

Output is just 1 — a good sanity check.

even max

Last row would be even

Subtract 1 (as in Example 2) so the final length stays odd.

Bad input

Convert.ToInt32 throws

Prefer int.TryParse so non-numeric input does not crash the program.

Time and Space Complexity

ProgramTimeExtra space
Consecutive digits (Examples 1–2)O(n²)O(1)
Odd digits (Example 3)O(n²)O(1)

For maximum length n (odd), total prints are 1+3+5+…+n = ((n+1)/2)².

Key Takeaways

  • Rule: for odd i up to max, print digits 1..i with Write.
  • Step by 2: i += 2 is what makes row lengths odd.
  • Write vs WriteLine: digits stay on the line; WriteLine advances after each row.
  • Next step: Program 23 mixes numbers with asterisks in a mirror layout.

One line: for each odd length up to max, print 1..i with Write, then WriteLine.

Frequently Asked Questions

Because the outer loop uses i += 2, so i becomes 1, 3, 5, 7, 9 — each row prints one more odd count of digits than the previous.
Console.Write(j) prints each digit immediately after the previous one on the same line — no space character is added.
Yes. Change the inner loop to j += 2 and print j to output 1, 13, 135, 1357, 13579 (see Example 3).
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.
Because the outer loop condition is i <= 9. Change 9 to any odd maximum to extend the pattern.
Subtract 1 to make it odd (see Example 2) so the last row still has an odd length.
O(n²) for maximum row length n because total prints are 1+3+5+…+n.
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?

Each row length increases by 2 because the outer loop uses i += 2 (1, 3, 5, 7, 9). The inner loop prints 1..i with no spaces — total prints grow as O(n²) for maximum row length n.

Next: Number & Asterisk Mirror

Mix digits with stars in a mirrored left–right layout.

Program 23 tutorial →

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