Left-Shifted Odd Number Triangle in C#

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

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:

Output
13579
3579
579
79
9
1

Complete 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.

C#
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

1

Outer loop visits odd starts

i += 2 makes i take values 1, 3, 5, 7, 9.

Row control
2

Inner loop prints odd numbers

j += 2 prints only odd values from i up to 9.

Odd-only
3

New line

Console.WriteLine() moves to the next row.

Line break
=

Left-shifted odd triangle

Each next row starts later, so the triangle shifts left.

2

Variation — Custom Maximum

Read the maximum value and print odd numbers up to that value:

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 = 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

1

Using += 2 makes the loop visit only odd numbers.

2

Increasing the start value each row shifts the triangle left.

3

You can generalize the idea to even numbers or different step sizes.

4

Complexity is still \(O(n^2)\) for large maximum values.

❓ Frequently Asked Questions

On the last iteration, i = 9, so the inner loop prints only 9 once.
Not with += 2 starting from an odd number, because that visits only odd values. To include 10, use j++ or handle even values separately.
O(n²) for maximum value n, even though only half the numbers are visited.

Explore More C# Number Patterns!

Step-size loops (like += 2) are a powerful way to generate patterns efficiently.

All Number Patterns →
Did you know?

Odd numbers form the sequence \(2k-1\). Printing only odd values is a quick way to create patterns with predictable spacing and symmetry.

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