Increasing odd-length number rows print consecutive digits 1..i with no spaces, where row lengths grow by 2: 1, 3, 5, 7, 9.
Remember
Rule: for odd i = 1, 3, 5, … max_n
print digits 1..i (no spaces)
1
123
12345
1234567
123456789 ← max_n = 9
The outer loop’s range(..., 2) is the whole trick — a clear follow-up after the jump triangle in Program 21.
Approach
How to Solve It
Walk odd lengths with range(..., 2); on each row print j from 1 to i with end="".
Method
Idea
Best for
Outer step 2
range(..., 2); inner 1..i
Learning, interviews
input() max
Same loops; force odd max if needed
Interactive practice
Odd-only digits
Inner loop also uses step 2
Variant practice
Pseudocode
Pseudocode
for i from 1 to max_n step 2:
for j from 1 to i:
print j (same line, no spaces)
print newline
Cheat sheet
Goal
Pattern
Set max
max_n = 9
Outer loop
for i in range(1, max_n + 1, 2):
Inner loop
for j in range(1, i + 1):
Print digit
print(j, end="")
End row
print()
Force odd max
if max_n % 2 == 0: max_n -= 1
Odd digits only
for j in range(1, i + 1, 2):
Printing Numbers vs Starting a New Line
API
Effect
Use for
print(j, end="")
Stays on the same line, no space
Each digit
print()
Ends the current line
After the inner loop
Glue digits with end="", then break once with print(). Using end=" " adds spaces; putting print() inside the inner loop prints one digit per line.
Try it
Live Preview
Change the maximum odd length and the pattern updates instantly. Even values are adjusted down by 1.
Whole numbers from 1 to 15. Even inputs become the next lower odd value.
Live resultmax = 9 · digits = 25
1
123
12345
1234567
123456789
Trace
Worked Walkthrough — max_n = 5
Trace each odd value of i and the digits concatenated on that row.
i
Inner j
Printed row
1
1
1
3
1 2 3
123
5
1 2 3 4 5
12345
Next odd i would be 7 — row length grows by exactly 2 each time.
Code
Python Programs
Three complete programs: fixed max_n = 9, input() max, and odd-only digits. Use View Output for sample results.
Example 1 — Fixed max_n = 9
Hard-coded maximum — outer step 2, inner concatenates 1..i.
Python
max_n = 9
for i in range(1, max_n + 1, 2):
for j in range(1, i + 1):
print(j, end="")
print()
Output
1
123
12345
1234567
123456789
How It Works
1. Outer step.i runs 1, 3, 5, 7, 9 — those are the row lengths.
2. Inner digits. Print j from 1 to i with end="" so digits glue together.
3. End the row.print() moves to the next line after the digit string is done.
Example 2 — input() Max
Read the maximum at runtime; if it is even, subtract 1 so the last row stays odd-length.
Python
max_n = int(input("Enter the maximum value: "))
if max_n % 2 == 0:
max_n -= 1
for i in range(1, max_n + 1, 2):
for j in range(1, i + 1):
print(j, end="")
print()
Output (when user enters 7)
Enter the maximum value: 7
1
123
12345
1234567
How It Works
1. Read max. Convert the prompt answer with int().
2. Oddify if needed. If even, max_n -= 1 keeps an odd last length.
3. Safer input tip. Bare int(input()) raises ValueError on letters. Prefer:
Safer input
raw = input("Enter the maximum value: ").strip()
try:
max_n = int(raw)
except ValueError:
print("Enter a positive whole number.")
raise SystemExit(1)
if max_n < 1:
print("Enter a positive whole number.")
raise SystemExit(1)
Example 3 — Odd-Only Digits range(..., 2)
Keep max_n = 9 but print only odd digits on each row.
Python
max_n = 9
for i in range(1, max_n + 1, 2):
for j in range(1, i + 1, 2):
print(j, end="")
print()
Output
1
13
135
1357
13579
How It Works
1. Same outer loop. Row lengths are still 1, 3, 5, 7, 9.
2. Skip evens. Inner range(..., 2) prints only 1, 3, 5, … up to i.
3. Compare shapes. Rows are shorter than Example 1 because half the digits are skipped.
Edge Cases & Pitfalls
Check these before calling the solution done.
no step
Forgot step 2
Using range(1, max_n + 1) prints every length 1, 2, 3, … — not only odd lengths. Keep step 2.
spaces
Extra spaces
Use end="", not end=" ", if you want tight digit strings like 12345.
print() inside
Vertical digits
If print() is inside the inner loop, each digit lands on its own line.
even max
Even maximum
With max_n = 8, the last odd i is 7 — or call max_n -= 1 first so intent is explicit.
max_n = 1
Single digit
Output is just 1 on one line.
input()
Catch ValueError
Letters crash bare int(input()) — wrap in try/except and require max_n >= 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Examples 1–2
O(n²)
O(1)
Example 3
O(n²)
O(1) (fewer digits, same order)
Total digits for odd lengths = 1 + 3 + 5 + … + n ≈ (n/2)² → still O(n²).
Remember
Key Takeaways
Rule: for each odd i, glue digits 1..i with end="".
Step 2:range(..., 2) makes lengths 1, 3, 5, 7, 9.
end= vs print(): digits stay on the line; print() advances after each row.
Complexity:O(n²) for maximum length n.
One line: step the outer loop by 2, and glue digits 1 through i together on each row.
Frequently Asked Questions
Because the outer loop uses range with step 2, so i becomes 1, 3, 5, 7, 9 — each row prints one more odd count of digits than the previous.
print(j, end="") prints each digit immediately after the previous one on the same line — no space character is added.
Yes. Change the inner loop to range(1, i + 1, 2) to output 1, 13, 135, 1357, 13579 (see Example 3).
print(j, end="") stays on the same line with no trailing space. print() ends the current line. Digits use end=""; the row break uses print() after the inner loop.
Because the outer loop runs while i <= 9 (range(1, 10, 2)). 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.
Use try/except ValueError around int(input()) so bad input does not crash the script.
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 range(..., 2) (1, 3, 5, 7, 9). The inner loop prints 1..i with end="" — total prints grow as O(n²) for maximum row length n.