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.
Approach
How to Solve It
One nested-loop idea with two useful variants — consecutive digits and odd-only digits.
Method
Idea
Best for
Odd row lengths
Outer i += 2; inner prints 1..i with Write
Learning, interviews, exams
Odd digits only
Same outer loop; inner also uses j += 2
Practice 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
Goal
Pattern
Odd row lengths
for (i = 1; i <= max; i += 2)
Print digits 1..i
for (j = 1; j <= i; j++) Console.Write(j);
End the row
Console.WriteLine();
Odd digits only
for (j = 1; j <= i; j += 2) Console.Write(j);
Even max from user
if (max % 2 == 0) max -= 1;
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each digit j (no space)
Console.WriteLine
Ends the current line
After the inner loop
Try it
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 resultmax 9 · 25 digits
1
123
12345
1234567
123456789
Trace
Worked Walkthrough — max = 7
Trace each odd outer-loop value of i and the digits the inner loop prints.
i
Inner j
Printed row
Digits
1
1..1
1
1
3
1..3
123
3
5
1..5
12345
5
7
1..7
1234567
7
Total digits are 1+3+5+7 = 16 — the sum of the first few odd numbers.
Code
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();
}
}
}
}
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();
}
}
}
}
Output (when user enters 7)
1
123
12345
1234567
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();
}
}
}
}
Output
1
13
135
1357
13579
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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)².
Remember
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.