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.
Approach
How to Solve It
Outer loop walks odd lengths with i += 2. Inner loop prints 1..i with cout << j (no spaces).
Method
Idea
Best for
Outer i += 2
for (i = 1; i <= max; i += 2) then print 1..i
Learning, interviews, exams
Odd-only digits
Same 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
Goal
Pattern
Walk odd lengths
for (i = 1; i <= max; i += 2)
Print 1..i
for (j = 1; j <= i; j++) cout << j;
End of row
cout << "\n";
Force odd max
if (max % 2 == 0) max -= 1;
Odd digits only
for (j = 1; j <= i; j += 2)
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << j
Stays on the same line
Each digit on the row
cout << "\n"
Ends the current line
After the inner loop finishes
Print digits without a newline, then end the row once.
Try it
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 resultmax = 9 · 25 digits
1
123
12345
1234567
123456789
Trace
Worked Walkthrough — max = 9
Trace how i += 2 grows the row length and 1..i fills each line.
i
Inner range
Prints
1
j = 1..1
1
3
j = 1..3
123
5
j = 1..5
12345
7
j = 1..7
1234567
9
j = 1..9
123456789
Total digits = 1 + 3 + 5 + … + n (odd sum) → O(n²) for max length n.
Code
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;
}
Output
1
123
12345
1234567
123456789
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;
}
Output (when user enters 7)
Enter the maximum value: 7
1
123
12345
1234567
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;
}
Output
1
123
12345
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
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.