Odd-Length Increasing Number Pattern in C++

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

What You’ll Learn

How to print this number pattern in C++ using nested loops:

1, 123, 12345, 1234567, 123456789

The trick is to increase the row length by 2 each time (odd lengths: 1, 3, 5, 7, 9).

⭐ Pattern Output

For the last row ending at 9, the pattern looks like this:

Output
1
123
12345
1234567
123456789
1

Complete C++ Program

The outer loop increments by 2 to keep rows odd-length. The inner loop prints digits from 1 to i.

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

int main() {
    int i, j;

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

    return 0;
}

🧠 How It Works

1

Choose odd row lengths

for (i = 1; i <= 9; i += 2) sets row lengths to 1, 3, 5, 7, and 9.

Row control
2

Print 1 to i

for (j = 1; j <= i; j++) prints 123...i for the current row.

Number printing
3

New line

cout << "\\n"; moves to the next row.

Line break
=

Odd-length rows

By stepping i by 2, each row grows by two digits, staying odd.

2

Variation — User Input Version

Let the user choose the maximum odd row length (e.g., 9, 11, 13).

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

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

    cout << "Enter the maximum odd row length: ";
    cin >> maxOdd;

    if (!cin || maxOdd <= 0) return 0;
    if (maxOdd % 2 == 0) maxOdd--; // make it odd
    if (maxOdd <= 0) return 0;

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Add spaces for readability: print cout << j << ' ';
  • Start from a different digit (e.g., 0 or 5) by printing start + j - 1
  • Print the same idea with even lengths by using i += 2 starting from 2
  • Right-align by printing leading spaces before each row
  • Change digits to characters to create alphabet sequences

Avoid

  • Using endl in tight loops (unnecessary flushing)
  • Letting maxOdd be even (convert it to odd before looping)
  • Not validating user input when using cin
  • Mixing up loop bounds (inner loop should run up to i)

Key Takeaways

1

Increase the row length by 2 to keep it odd: 1, 3, 5, 7, 9.

2

The inner loop prints from 1 to the current row limit i.

3

This pattern is a clean nested-loop exercise for building row/column thinking.

4

It can be easily adapted to print spaced digits, shifted starts, or aligned output.

❓ Frequently Asked Questions

Because the outer loop increases the row limit by 2 each time (1, 3, 5, ...). The inner loop then prints j from 1 to that limit.
Change the print statement to cout << j << ' ';. That will output 1 2 3 instead of 123.
Yes. Use the user-input version and set a larger odd maximum like 11, 13, or 15. (Note: for values above 9, you’ll print multi-digit numbers which changes the visual width.)
O(n²) in terms of the number of rows, because you print a triangular number of digits overall.

Explore More C++ Number Patterns!

Odd-length rows are a nice way to practice loop stepping (i += 2) and inner-loop bounds.

All Number Patterns →
Did you know?

Using i += 2 is a simple technique to iterate only over odd values (1, 3, 5, …), which is handy in many pattern-printing tasks.

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