Left-Shifted Odd Number Triangle in C#

What You’ll Learn
How to print a left-shifted odd number triangle in C#. Each row prints consecutive odd numbers, and the starting odd number increases each row: 13579, 3579, 579, 79, 9.
This pattern is a great way to practice nested loops with a step size of 2.
⭐ Pattern Output
The pattern for max = 10 looks like this:
13579
3579
579
79
9Complete C# Program
The outer loop chooses the first odd number in each row. The inner loop prints odd numbers up to 9 by stepping with j += 2.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j;
for (i = 1; i <= 10; i += 2)
{
for (j = i; j <= 10; j += 2)
{
Console.Write(j);
}
Console.WriteLine();
}
}
}
}🧠 How It Works
Outer loop visits odd starts
i += 2 makes i take values 1, 3, 5, 7, 9.
Inner loop prints odd numbers
j += 2 prints only odd values from i up to 9.
New line
Console.WriteLine() moves to the next row.
Left-shifted odd triangle
Each next row starts later, so the triangle shifts left.
Variation — Custom Maximum
Read the maximum value and print odd numbers up to that value:
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 = i; j <= max; j += 2)
{
Console.Write(j);
}
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Print even numbers by starting at 2 and using
+= 2 - Add spaces between numbers for clarity
- Right-align the triangle by printing leading spaces
Avoid
- Using
j++(that would include even numbers too) - Forgetting
Console.WriteLine()after each row
Key Takeaways
Using += 2 makes the loop visit only odd numbers.
Increasing the start value each row shifts the triangle left.
You can generalize the idea to even numbers or different step sizes.
Complexity is still \(O(n^2)\) for large maximum values.
❓ Frequently Asked Questions
i = 9, so the inner loop prints only 9 once.+= 2 starting from an odd number, because that visits only odd values. To include 10, use j++ or handle even values separately.Explore More C# Number Patterns!
Step-size loops (like += 2) are a powerful way to generate patterns efficiently.
Odd numbers form the sequence \(2k-1\). Printing only odd values is a quick way to create patterns with predictable spacing and symmetry.
12 people found this page helpful
