Increasing Odd-Length Number Rows in C#

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:
1
123
12345
1234567
123456789Complete 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.
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
Increase by 2
The outer loop increments by 2, so row lengths are odd: 1, 3, 5, 7, 9.
Print 1..i
The inner loop prints numbers from 1 to the current row length i.
Odd-length growth
A simple step size change creates a new family of patterns.
Variation — User Input Maximum
Read the maximum value (make it odd if needed) and print the same pattern:
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
i += 2 creates odd row lengths.
The inner loop prints a simple sequence from 1 to i.
You can parameterize the maximum value easily.
Total work grows roughly like \(O(n^2)\).
❓ Frequently Asked Questions
i <= 9. Change 9 to any odd number to extend it.for (j = 1; j <= i; j += 2) and print j.Explore More C# Number Patterns!
Try changing the step size to generate even-length or custom-length patterns.
The sequence of odd numbers 1, 3, 5, 7, 9 controls how many items are printed per row in many pyramid-style patterns.
12 people found this page helpful
