Increasing Odd-Length Number Rows in C

Beginner
⏱️ 7 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Step Size Loop

What You’ll Learn

The increasing odd-length number rows pattern uses i += 2 in the outer loop so each row prints 1..i with lengths 1, 3, 5, 7, 9 — a natural step after the jump triangle in Program 21. This tutorial covers the shape rule, step-size logic, a live preview, algorithm steps, worked C examples, edge cases, and complexity.

Shape Rule

Odd row lengths

Row 1 prints 1, row 2 prints 123, row 3 prints 12345, and so on — digits run together with no spaces.

Outer Loop

i += 2

for (i = 1; i <= max; i += 2) walks odd values 1, 3, 5, 7, 9 as row lengths.

Inner Loop

1..i

for (j = 1; j <= i; j++) then printf("%d", j) — no space between digits.

printf vs newline

Same line / next line

Digits use printf("%d", j); end each row with printf("\n").

Live Preview

1–15 max

Pick an odd maximum and draw the odd-length rows pattern instantly in the browser.

O(n²)

Complexity

Total prints = 1+3+5+...+max; extra memory stays O(1).

Introduction

An increasing odd-length number rows pattern prints consecutive digits 1..i on each row, with row lengths growing by 2 each time. With max = 9, the output is 1, 123, 12345, 1234567, 123456789.

In C you use for (i = 1; i <= max; i += 2) in the outer loop and printf("%d", j) in the inner loop, then printf("\n") ends each row.

Why it matters?

It introduces loop step sizes — a simple change to i += 2 creates a whole new family of patterns.

Key Highlights

Outer i += 2

Row lengths are 1, 3, 5, 7, 9 — always odd.

Inner 1..i

printf("%d", j) concatenates digits on one line.

No spaces

Digits run together — 123 not 1 2 3.

Series Foundation

Follow Program 21; continue to Program 23 (number & asterisk mirror) next.

In short: for each odd i up to max, print digits 1 through i with printf("%d", j), then call printf("\n").

📝 Problem & Approach

Given a positive odd integer max, print increasing odd-length rows: for each odd i from 1 to max, print digits 1 through i concatenated on one line.

c
// max = 9 (conceptual shape)
// 1
// 123
// 12345
// 1234567
// 123456789

Inputs & Outputs

ItemTypeDescription
maxintMaximum row length (typically odd, e.g. 9).
iintOuter loop — odd values 1, 3, 5, … up to max.
jintInner loop — prints digits 1 through i.
Printed outputtextRow i has i concatenated digits — no spaces.

Minimal workflow

Pseudocode
for i from 1 to max step 2:
    for j from 1 to i:
        print j (no space)
    print newline

Approach comparison

ApproachIdeaBest for
Outer i += 21, 123, 12345, …Learning and interviews
User-input maxscanf("%d", &max);Flexible console programs
Inner j += 21, 13, 135, 1357, …Odd-only digit rows

⚡ Quick Reference

GoalPattern
Walk odd lengthsfor (i = 1; i <= max; i += 2)
Print digitsfor (j = 1; j <= i; j++)
Write digitprintf("%d", j);
End the rowprintf("\n");
Odd-only variantfor (j = 1; j <= i; j += 2)
User inputscanf("%d", &max);

📋 Fixed Max vs User Input vs Odd-Only Digits

Same odd-length rows — different ways to control max and inner loop step.

Outer step
i += 2

Row lengths 1, 3, 5, 7, 9

Inner 1..i
printf("%d", j)

Concatenate digits — no spaces

Odd digits
j += 2

Print 1, 3, 5 only in Example 3

Learning tip
even max

Subtract 1 if user enters an even maximum

Context

When This Pattern Shows Up

Reach for this pattern when teaching loop step sizes and concatenated digit output inside nested loops.

  1. Post-jump exercise

    Natural follow-up after Program 21 — introduces outer loop step i += 2.

  2. Nested-loop warm-up

    Outer/inner bound practice with an immediate visual check.

  3. Console I/O practice

    Combine loops with scanf for a flexible row count.

  4. Gateway to variants

    Compare Program 21 (jump triangle) and Program 23 (number & asterisk mirror) next.

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.

🔮 Live Preview

Choose an odd maximum between 1 and 15 and draw the odd-length rows pattern in the browser.

Try 7, 9, or 11. Even values are adjusted down by 1. Max up to 15.

Live result
Press "Draw pattern".

Examples Gallery

Three complete C programs — fixed maximum, user input, and odd-only digits variant. Click View Output to reveal sample console results.

📚 Getting Started

Print five rows of odd-length consecutive digits with i += 2.

Example 1 — Fixed max = 9

Hard-coded maximum — ideal for first demos and screenshots.

c
#include <stdio.h>

int main() {
    int i, j;

    for (i = 1; i <= 9; i += 2) {
        for (j = 1; j <= i; ++j)
            printf("%d", j);
        printf("\n");
    }

    return 0;
}

How It Works

When i = 1, the inner loop prints 1 once. When i = 3, j runs 1, 2, 3 — output 123. When i = 9, digits 1 through 9 concatenate into 123456789. printf("\n") after the inner loop starts the next row.

📈 User Input

Read the maximum with scanf instead of hard-coding 9.

Example 2 — User Input

Read max with scanf("%d", &max); adjust to odd if the user enters an even value.

c
#include <stdio.h>

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

    printf("Enter the maximum value: ");
    scanf("%d", &max);

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

    for (i = 1; i <= max; i += 2) {
        for (j = 1; j <= i; ++j)
            printf("%d", j);
        printf("\n");
    }

    return 0;
}

How It Works

Same nested-loop core as Example 1; only the source of max changes. The if (max % 2 == 0) max -= 1 guard keeps the last row odd-length. Non-numeric input leaves max unset if you ignore scanf’s return value — always check it in safer labs.

⚡ Odd-Only Digits

Use j += 2 in the inner loop to print only odd digits.

Example 3 — Odd-Only Digits j += 2

Keep max = 9 but print 1, 3, 5, 7, 9 instead of 1..i on each row.

c
#include <stdio.h>

int main() {
    int i, j;

    for (i = 1; i <= 9; i += 2) {
        for (j = 1; j <= i; j += 2)
            printf("%d", j);
        printf("\n");
    }

    return 0;
}

How It Works

Change only the inner loop to j += 2 — the outer loop and printf("\n") logic stay the same. Each row prints odd digits up to i instead of every digit from 1 to i.

🧠 How the Algorithm Prints Rows

1

Set up

#include <stdio.h> brings in printf / scanf. Set max and loop variables i, j.

Setup
2

Outer loop + i += 2

for (i = 1; i <= max; i += 2) — row lengths are 1, 3, 5, 7, 9.

Row
3

Inner loop + printf("%d", j)

for (j = 1; j <= i; j++) then printf("%d", j) — digits concatenate with no spaces.

Digits
4

New line

printf("\n") ends the row so the next outer iteration starts fresh.

Break
=

Odd-length rows complete

Total prints grow as 1+3+5+...+maxO(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — max = 9

Trace each outer-loop value of i and the digits printed on each row.

iInner j rangeDigits printedRow output
1111
31..31, 2, 3123
51..51, 2, 3, 4, 512345
71..71..71234567
91..91..9123456789

Total digit prints: 1 + 3 + 5 + 7 + 9 = 25.

Use Cases

Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.

1. Teaching Nested Loops

Clearest visual proof that outer and inner bounds interact.

Example: change j <= i and watch the shape change.

2. Pattern Series Base

Foundation for inverted, pyramid, diamond, and hollow variants.

Example: use (i + j) % 2 for row+column parity grids.

3. Console Formatting Drills

Practice printf vs row newline without complex math.

Example: put printf("\n") inside the inner loop by mistake.

4. Character Substitution

Swap digits for letters, stars, or spaced output once the loop works.

Example: print j + " " for spaced digits on each row.

5. Complexity Intuition

Triangular totals make O(n²) concrete for beginners.

Example: count printed digits for n = 10 still → 55.

6. Input Validation Labs

Pair the pattern with scanf return checks and positive-row checks.

Example: reject max <= 0 and re-prompt.

Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.

Advantages

Why this pattern earns a permanent spot in beginner C courses.

  1. 1. Instant Visual Feedback

    Wrong bounds show up immediately as a broken staircase.

  2. 2. Minimal Concepts

    Only loops and console output — no arrays or math libraries.

  3. 3. Easy to Extend

    Invert, center, hollow, or change the fill character with small edits.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop counters.

Pro Tip: learn i += 2 in the outer loop first; compare with j += 2 odd-digit variant in Example 3.

Usage Tips

Small habits that keep number-pattern code clean.

  1. 1. Name Bounds Clearly

    Use printf("%d", j) without spaces — not printf("%d ", j) unless you want gaps.

  2. 2. Prefer scanf

    Check the return value so bad input does not leave max uninitialized.

  3. 3. Keep printf("\n") Outside

    Only call printf("\n") after the inner loop finishes the row.

  4. 4. Trace i and j on Paper

    Write each odd i and the j range before coding.

  5. 5. Dry-Run One Small n

    Trace max = 7 on paper before coding larger demos.

Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put printf("\n") inside the inner loop.

Common Pitfalls

Mistakes that commonly break odd-length row patterns.

  1. 1. Newline Inside the Inner Loop

    Each digit lands on its own line — you get a column, not a triangle.

    → Use printf("%d", j); printf("\n") only after the inner loop.

  2. 2. Forgetting i += 2

    Using i++ prints every length 1, 2, 3, 4 — not odd lengths only.

    → Use for (i = 1; i <= max; i += 2) for odd row lengths.

  3. 3. Adding Spaces Between Digits

    printf("%d ", j) produces 1 2 3 instead of 123.

    → Use printf("%d", j) for concatenated digits.

  4. 4. Forgetting the Row Break

    Omitting printf("\n") glues every number onto one endless line.

    → Always end the row after the inner loop.

  5. 5. Unchecked scanf

    Letters or empty input leave max uninitialized.

    → Check scanf return value and re-prompt on failure.

Edge Cases

Check these inputs before calling the solution done.

max = 1

Single digit

Output is just 1 on one line.

max = 0

Empty pattern

Outer loop never runs — print nothing or show a message.

Negative

max < 0

Treat as invalid; re-prompt instead of silent empty output.

Large n

Large width

Output grows as n² characters — fine for labs, noisy for huge n.

Bad input

Non-numeric scanf input

Unchecked scanf leaves max unset — check the return value.

Even max

User enters even

Subtract 1 to keep odd row lengths — see Example 2.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Jump number triangle

2. Number & asterisk mirror

3. Spaced digits

  • Use printf("%d ", j) for gaps
  • Compare with concatenated output

4. No trailing space

  • Print space only between numbers, not after the last
  • Harder follow-up after this page

Notes

  • Odd sum. Total prints = 1+3+5+...+max — O(n²) for maximum row length n.
  • printf("%d", j) stays on the line; printf("\n") advances — mix them carefully.
  • Validate max > 0 for interactive programs; max = 1 should print a single 1.
  • This page is left-aligned. Centered pyramids need leading spaces — covered later in the series.

Quick Takeaway: use i += 2 in the outer loop, printf("%d", j) in the inner loop, then printf("\n") after each row.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(max²)O(1)
Odd digits (Example 3)O(max²)O(1)
Wrap Up

🎉 Conclusion

The increasing odd-length number rows pattern is a compact lesson in loop step sizes: use i += 2 in the outer loop and printf("%d", j) in the inner loop to concatenate digits. Master the fixed-max version, then try user input and the odd-only digit variant.

Practice the three examples above, then continue to Program 23 for the number & asterisk mirror pattern.

Use i += 2 for odd lengths — validate max and adjust even input when reading from the console.

💡 Best Practices

✅ Do

  • Use for (i = 1; i <= max; i += 2) in the outer loop
  • Use printf("%d", j) — no space between digits
  • Adjust even max with max -= 1 for user input
  • Check scanf return value before using max
  • State O(n²) time when asked about complexity

❌ Don’t

  • Call printf("\n") inside the inner digit loop
  • Use i++ when you want odd lengths only
  • Add spaces unless you want separated digits
  • Skip the newline after each row
  • Ignore bad console input in user-facing demos
  • Skip the max = 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this odd-length pattern

Print the pattern the beginner-friendly way.

5
Core concepts
02

Inner 1..i

printf("%d", j) no space

Code
+ 03

Odd lengths

1, 3, 5, 7, 9

Shape
04

j += 2

Odd digits variant

Variant
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

Because the outer loop uses i += 2, so i becomes 1, 3, 5, 7, 9 — each row prints one more odd count of digits than the previous.
printf("%d", j) prints each digit immediately after the previous one on the same line — no space character is added.
Yes. Change the inner loop to j += 2 and print j to output 1, 13, 135, 1357, 13579 (see Example 3).
printf("%d", j) stays on the same line. printf("\n") ends the current line. Digits use printf; the row break uses printf("\n") after the inner loop.
Because the outer loop condition is i <= 9. Change 9 to any odd maximum to extend the pattern.
Subtract 1 to make it odd (see Example 2) so the last row still has an odd length.
O(n²) for maximum row length n because total prints are 1+3+5+...+n.
Check scanf's return value: if (scanf("%d", &max) != 1) handle bad input. Unchecked scanf leaves max uninitialized on failure.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

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.

Continue to Program 23

Move on to the number & asterisk mirror pattern in the C number-pattern series.

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