Left-Shifted Odd Number Triangle in C++

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print an odd-number pattern that shifts left each row in C++:

13579, 3579, 579, 79, 9

We do this by stepping through odd numbers using += 2 in both the outer and inner loops.

⭐ Pattern Output

For odd numbers from 1 to 9, the pattern looks like this:

Output
13579
3579
579
79
9
1

Complete C++ Program

The outer loop chooses the starting odd number for each row (1, 3, 5, 7, 9). The inner loop prints from that start up to the maximum odd value (9).

C++
#include <iostream>
using namespace std;

int main() {
    int maxOdd = 9;
    int i, j;

    for (i = 1; i <= maxOdd; i += 2) {
        for (j = i; j <= maxOdd; j += 2) {
            cout << j;
        }
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Choose the maximum odd number

int maxOdd = 9; sets the last number printed in the first row.

Setup
2

Outer loop (start value per row)

for (i = 1; i <= maxOdd; i += 2) visits only odd starts: 1, 3, 5, 7, 9.

Row control
3

Inner loop (print odd numbers)

for (j = i; j <= maxOdd; j += 2) prints odd numbers from the current start to maxOdd.

Number printing
4

New line

cout << "\\n"; moves to the next row after printing each line.

Line break
=

Left-shifted odd triangle

Each row starts later, so the printed sequence shortens by one odd number each line.

2

Variation — User Input Version

Let the user enter the maximum number. If the input is even, we reduce it by 1 so the pattern ends on an odd number.

C++
#include <iostream>
using namespace std;

int main() {
    int maxOdd;
    int i, j;

    cout << "Enter the maximum number: ";
    cin >> maxOdd;

    if (!cin || maxOdd <= 0) return 0;
    if (maxOdd % 2 == 0) maxOdd -= 1;

    for (i = 1; i <= maxOdd; i += 2) {
        for (j = i; j <= maxOdd; j += 2) {
            cout << j;
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Print spaces between numbers for readability: cout << j << ' ';
  • Start from a different first odd number (like 3) for a shifted first row
  • Increase the maximum odd number (11, 13, 15...) for a bigger triangle
  • Right-align by printing leading spaces before each row
  • Print odd squares or other sequences instead of consecutive odds

Avoid

  • Using an even maximum without converting it to odd (the last odd would be skipped)
  • Forgetting += 2 (you’ll print even numbers too)
  • Using endl inside loops (unnecessary flushing)
  • Skipping the newline after each row

Key Takeaways

1

Use += 2 to iterate through only odd numbers.

2

The outer loop sets the row’s starting value (1, 3, 5, ...).

3

The inner loop prints from the start odd up to the maximum odd value.

4

Each row prints fewer values, creating a left-shifted triangle.

❓ Frequently Asked Questions

Both loops increase by 2, so they visit 1, 3, 5, 7, 9—only odd values.
Because the outer loop increases the starting odd number each row: 1, 3, 5, 7, 9. That skips earlier odds and shortens each line.
Yes. Set maxOdd to 11, 13, 15, etc. If the user enters an even number, convert it to odd with maxOdd--.
The amount of printing grows quadratically as you increase the maximum value, so it’s typically described as O(n²).

Explore More C++ Number Patterns!

Odd-number patterns are great practice for stepping through sequences and controlling loop ranges.

All Number Patterns →
Did you know?

If you want only odd numbers, you don’t need a conditional check—just start from 1 and increment by 2.

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