C++ Number Rows Pattern (Odd Length)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

Increasing odd-length number rows print consecutive digits 1..i on each line, with row lengths growing by 2 (1, 3, 5, 7, 9).

Remember
Rule: for i from 1 to max step 2
        print j from 1 to i (no spaces)

1
123
12345
1234567
123456789     ← max = 9

Follows the jump number triangle in Program 21; next is the number & asterisk mirror in Program 23.

How to Solve It

Outer loop walks odd lengths with i += 2. Inner loop prints 1..i with cout << j (no spaces).

MethodIdeaBest for
Outer i += 2for (i = 1; i <= max; i += 2) then print 1..iLearning, interviews, exams
Odd-only digitsSame outer loop; inner uses j += 2 for 1, 13, 135…Variant that prints only odd digits

Pseudocode

Pseudocode
for i from 1 to max step 2:
    for j from 1 to i:
        print j
    print newline

Cheat sheet

GoalPattern
Walk odd lengthsfor (i = 1; i <= max; i += 2)
Print 1..ifor (j = 1; j <= i; j++) cout << j;
End of rowcout << "\n";
Force odd maxif (max % 2 == 0) max -= 1;
Odd digits onlyfor (j = 1; j <= i; j += 2)

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << jStays on the same lineEach digit on the row
cout << "\n"Ends the current lineAfter the inner loop finishes

Print digits without a newline, then end the row once.

Live Preview

Change the maximum length and the odd-length rows update instantly — even values snap down to odd; capped at 9 for readable demos.

Whole numbers from 1 to 9. Even values are adjusted down by 1. Tap a chip or type — the preview redraws as you go.

Live result max = 9 · 25 digits
1
123
12345
1234567
123456789

Worked Walkthrough — max = 9

Trace how i += 2 grows the row length and 1..i fills each line.

iInner rangePrints
1j = 1..11
3j = 1..3123
5j = 1..512345
7j = 1..71234567
9j = 1..9123456789

Total digits = 1 + 3 + 5 + … + n (odd sum) → O(n²) for max length n.

C++ Programs

Three complete programs: fixed max = 9, cin input, and a compact max = 5 demo. Use View Output to reveal sample results.

Example 1 — Fixed max = 9

Hard-coded maximum — outer i += 2, inner prints 1..i with no spaces.

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

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

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

    return 0;
}

How It Works

1. Outer steps by 2. i becomes 1, 3, 5, 7, 9 — each value is the row length.

2. Inner fills 1..i. cout << j concatenates digits with no spaces.

3. Newline once. Call cout << "\n" only after the inner loop finishes.

Example 2 — User Input Max

Read max with cin, validate, and force odd so the last row stays odd-length.

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

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

    cout << "Enter the maximum value: ";
    if (!(cin >> max) || max <= 0)
    {
        cout << "Please enter a positive integer.\n";
        return 1;
    }

    if (max % 2 == 0)
        max -= 1;

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

    return 0;
}

How It Works

1. Prompt and validate. Reject failed reads and non-positive values before printing.

2. Force odd. if (max % 2 == 0) max -= 1; keeps the last row odd-length.

3. Safer input tip. Cap demos for readable output:

Safer input
if (!(cin >> max) || max < 1 || max > 9)
{
    cout << "Enter a whole number from 1 to 9.\n";
    return 1;
}

Example 3 — Compact max = 5

Same structure with only three rows — easy to confirm i += 2 lengths.

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

int main()
{
    int max = 5;
    int i, j;

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

    return 0;
}

How It Works

1. Three rows. i = 1 → 1; i = 3 → 123; i = 5 → 12345.

2. Trace on paper. If you use i++ instead of i += 2, you get every length — not odd-only.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for max = 9 or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

i++

Wrong step

Using i++ prints every length 1, 2, 3, 4… For odd lengths only, keep i += 2.

even max

Even maximum

If max is even, subtract 1 so the last row stays odd-length — see Example 2.

\n inside

Broken rows

If cout << "\n" sits inside the inner loop, each digit lands on its own line. Call it only after the row finishes.

max = 1

Single digit

Output is just 1 — a good sanity check for input validation.

spaces

Unwanted gaps

This pattern concatenates digits. Use cout << j, not cout << j << " ", unless you want spaced output.

cin

Check the stream

Validate cin >> max before looping — a failed read leaves max unset or stale.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(n²)O(1)
Compact max = 5 (Example 3)O(n²)O(1)

Total digits printed = 1 + 3 + 5 + … + n (sum of odds up to n) → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Step by 2: i += 2 gives odd row lengths 1, 3, 5, 7, 9.
  • Print 1..i: cout << j concatenates digits with no spaces.
  • Break the row: digits in the inner loop; cout << "\n" after it.
  • Complexity: O(n²) time from the odd-sum of digits; O(1) extra space.

One line: for each odd i up to max, print digits 1..i, then newline.

Frequently Asked Questions

Increasing odd-length rows: for max=9 you get 1 / 123 / 12345 / 1234567 / 123456789 — each row prints digits 1..i with lengths 1, 3, 5, 7, 9.
Because the outer loop uses i += 2, so i becomes 1, 3, 5, 7, 9 — each row has an odd digit count.
cout << j prints each digit immediately after the previous one — no space character is added.
Yes. Change the inner loop to j += 2 to output 1, 13, 135, 1357, 13579.
cout << j stays on the same line. cout << "\n" ends the current line. Digits use cout << j; the row break uses cout << "\n" after the inner loop.
Subtract 1 to make it odd so the last row still has an odd length.
After prompting, use if (!(cin >> max) || max <= 0) to reject bad input before printing.
O(n²) for maximum row length n because total prints are 1+3+5+…+n.

Did you know?

Each row length increases by 2 because the outer loop uses i += 2 (1, 3, 5, 7, 9). The inner loop prints 1..i with no spaces — total prints grow as O(n²) for maximum row length n.

Next: Number & Asterisk Mirror

Continue with a mirrored mix of digits and asterisks.

Program 23 tutorial →

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