C Number Rows Pattern (Odd Length)

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

What Is This Pattern?

An increasing odd-length number rows pattern prints digits 1..i on each row, where i steps by 2 — so row lengths are 1, 3, 5, 7, 9 with no spaces between digits.

Remember
Rule: for odd i = 1, 3, 5, …, max
      print digits 1 through i (no spaces)

1
123
12345
1234567
123456789     ← max = 9

The key idea is the outer step i += 2. Same nested-loop idea as a growing triangle, but only odd widths. Follows the jump triangle in Program 21; next up is Program 23.

How to Solve It

Start with consecutive digits 1..i, then optionally print only odd digits with an inner j += 2.

MethodIdeaBest for
Outer i += 2, inner 1..iOdd row lengths; print every digit up to iLearning, interviews, exams
Inner j += 2Same outer step; print only odd digitsVariant once the core loops click

Pseudocode

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

Cheat sheet

GoalPattern
Odd row lengthsfor (i = 1; i <= max; i += 2)
Print 1..ifor (j = 1; j <= i; j++) printf("%d", j);
End the rowprintf("\n");
Keep max oddif (max % 2 == 0) max -= 1;
Odd digits onlyfor (j = 1; j <= i; j += 2)

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d", j)Stays on the same lineEach digit
printf("\n")Ends the current lineAfter the inner loop

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

Live Preview

Change the maximum odd length and the pattern updates instantly — even values snap down to the previous odd.

Whole numbers from 1 to 19. Even inputs become the previous odd (e.g. 8 → 7).

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

Worked Walkthrough — max = 7

Trace each outer-loop value of i (1, 3, 5, 7) and list the digits the inner loop prints.

iInner jPrinted rowDigits
11..111
31..31233
51..5123455
71..712345677

Total digit prints: 1 + 3 + 5 + 7 = 16 = ((7 + 1) / 2)². That odd-sum square is why time is O(n²).

C Programs

Three complete programs: fixed max, scanf input, and an odd-digits-only variant. Use View Output to reveal sample results.

Example 1 — Fixed max = 9

Hard-coded maximum — outer step 2; inner loop concatenates digits with printf("%d", j).

C
#include <stdio.h>

int main(void)
{
    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

1. Outer loop picks odd lengths. i runs 1, 3, 5, 7, 9 because of i += 2.

2. Inner loop prints 1..i. For each i, j runs from 1 to i and printf("%d", j) concatenates digits.

3. Break the line. printf("\n") after the inner loop starts the next (longer) row.

When i = 1 you get 1; when i = 5 you get 12345; when i = 9 you get 123456789.

Example 2 — User Input Version

Read max with scanf. If the user enters an even value, drop to the previous odd so the last row stays odd-length.

C
#include <stdio.h>

int main(void)
{
    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

1. Prompt and read. Ask for a maximum, then store it with scanf("%d", &max).

2. Force an odd max. if (max % 2 == 0) max -= 1; — entering 8 becomes 7.

3. Same nested-loop core. Only the source of max changes — the print logic matches Example 1.

4. Safer input tip. Unchecked scanf leaves max uninitialized on bad input. Prefer:

Safer input
if (scanf("%d", &max) != 1 || max < 1)
{
    printf("Enter a positive whole number.\n");
    return 1;
}

Example 3 — Odd digits only (j += 2)

Keep the outer odd lengths, but print only odd digits — 1, 13, 135, …

C
#include <stdio.h>

int main(void)
{
    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

1. Same outer step. i still walks 1, 3, 5, 7, 9.

2. Inner step skips evens. j += 2 prints 1, 3, 5, … up to i instead of every digit.

3. Learn consecutive digits first. Use Examples 1–2 for the standard pattern; treat this as a small variation afterward.

Edge Cases & Pitfalls

Check these before calling the solution done.

i++

Every length, not odd

If the outer loop uses i++ instead of i += 2, you get all lengths 1..max. Keep the step at 2 for this pattern.

printf space

Spaced digits

printf("%d ", j) inserts spaces. Use printf("%d", j) with no trailing space for 12345.

\n inside

Column of digits

If printf("\n") is inside the inner loop, each digit lands on its own line. Call the newline only after the inner loop.

even max

Last row even

Without max -= 1 on even input, the last printed length is still odd (loop stops at the largest odd ≤ max), but clamping makes the intent explicit for students.

max = 1

Single digit

Output is just 1 — a good sanity check.

scanf

Check the return value

If scanf fails, max may be uninitialized — always test scanf(...) == 1.

Time and Space Complexity

ProgramTimeExtra space
Consecutive digits (Examples 1–2)O(max²)O(1)
Odd digits only (Example 3)O(max²)O(1)

Total digits for odd max n = 1 + 3 + … + n = ((n + 1) / 2)² — still quadratic in n.

Key Takeaways

  • Rule: for each odd i, print digits 1 through i with no spaces.
  • Step size: i += 2 is what makes lengths 1, 3, 5, …
  • Break the row: call printf("\n") only after the inner loop.
  • Complexity: O(n²) time from the odd-sum digit count; O(1) extra space.

One line: for odd i up to max, print 1..i with printf("%d", j), then printf("\n").

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("%d", j); 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 = ((n+1)/2)².
Check scanf's return value: if (scanf("%d", &max) != 1) handle bad input. Unchecked scanf leaves max uninitialized on failure.

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

Combine digits and stars in a mirrored layout.

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