Odd-length number rows print consecutive digits 1..i on each line, where row lengths grow by 2: 1, 3, 5, 7, 9.
Remember
Rule: for odd i from 1 to max,
print digits 1..i with no spaces
1
123
12345
1234567
123456789 ← max = 9
The outer loop uses i += 2 so only odd lengths appear. Natural step after Program 21 (jump triangle).
Approach
How to Solve It
Step the outer loop by 2; print 1..i with System.out.print(j).
Method
Idea
Best for
Odd lengths
Outer i += 2; inner prints 1..i
Learning, interviews, exams
Odd digits
Same outer loop; inner uses j += 2
Rows of only odd digits (1, 13, 135…)
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 (int i = 1; i <= max; i += 2)
Print 1..i
for (int j = 1; j <= i; j++) System.out.print(j);
No spaces
Use print(j), not print(j + " ")
End the row
System.out.println();
Even max fix
if (max % 2 == 0) max -= 1;
Odd digits only
for (int j = 1; j <= i; j += 2)
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each digit j (no space)
System.out.println
Ends the current line
After the inner loop
Print digits without a newline, then end the row once.
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. Tap a chip or type a value — the preview redraws as you go.
Live resultmax = 9 · 5 rows · 25 digits
1
123
12345
1234567
123456789
Trace
Worked Walkthrough — max = 7
Trace each odd i, the digit range, and the printed line.
i
Inner range
Digits
Printed row
1
1..1
1
1
3
1..3
3
123
5
1..5
5
12345
7
1..7
7
1234567
Total digits: 1 + 3 + 5 + 7 = 16. In general, for odd max n, the sum is ((n + 1) / 2)² — still O(n²).
Code
Java Programs
Three complete programs: fixed max = 9, Scanner input with even-max fix, and odd-only digits. Use View Output to reveal sample results.
Example 1 — Fixed max = 9
Hard-coded maximum — outer loop steps by 2; inner prints concatenated digits.
Java
public class OddLengthRows {
public static void main(String[] args) {
for (int i = 1; i <= 9; i += 2) {
for (int j = 1; j <= i; j++)
System.out.print(j);
System.out.println();
}
}
}
Output
1
123
12345
1234567
123456789
How It Works
1. Outer steps by 2.i takes values 1, 3, 5, 7, 9 — those are the row lengths.
2. Inner concatenates.System.out.print(j) with no space builds 123, 12345, and so on.
3. Break the line.System.out.println() after the inner loop starts the next row.
When i = 5, j runs 1–5 and the line is 12345.
Example 2 — Max Input
Read max at runtime. Adjust even values down so the last row stays odd-length.
Java
import java.util.Scanner;
public class OddLengthRowsInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the maximum value: ");
int max = sc.nextInt();
if (max % 2 == 0) max -= 1;
for (int i = 1; i <= max; i += 2) {
for (int j = 1; j <= i; j++)
System.out.print(j);
System.out.println();
}
sc.close();
}
}
Output (when user enters 7)
Enter the maximum value: 7
1
123
12345
1234567
How It Works
1. Prompt and read. Ask for a maximum, then store it with sc.nextInt().
2. Keep it odd.if (max % 2 == 0) max -= 1; so entering 8 becomes 7.
3. Safer input tip. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter a positive whole number.");
return;
}
int max = sc.nextInt();
if (max < 1) {
System.out.println("Enter a positive whole number.");
return;
}
Example 3 — Odd-Only Digits j += 2
Same outer loop; the inner loop prints only odd digits.
Java
public class OddLengthOddDigits {
public static void main(String[] args) {
for (int i = 1; i <= 9; i += 2) {
for (int j = 1; j <= i; j += 2)
System.out.print(j);
System.out.println();
}
}
}
Output
1
13
135
1357
13579
How It Works
1. Same outer loop. Row lengths are still 1, 3, 5, 7, 9.
2. Inner steps by 2.j += 2 prints 1, 3, 5, … up to i.
3. Shorter rows. When i = 5, digits are 1, 3, 5 → 135 (three characters, not five).
Edge Cases & Pitfalls
Check these before calling the solution done.
i++
Every length
Using i++ prints lengths 1, 2, 3, 4… Use i += 2 for odd lengths only.
Spaces
Wrong shape
print(j + " ") produces 1 2 3 instead of 123. Use bare print(j).
println inside
Column of digits
If println is inside the inner loop, each digit lands on its own line. Use print for digits; println only after the inner loop.
max = 1
Single 1
Output is just 1 — a good sanity check.
Even max
Adjust down
Without the even fix, i <= 8 still works but the last odd length is 7. Explicitly subtract 1 for clarity.
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require a positive whole number.
Analysis
Time and Space Complexity
Program
Time
Extra space
Odd lengths (Examples 1–2)
O(n²)
O(1)
Odd digits (Example 3)
O(n²)
O(1)
Total digit prints for odd max n: 1 + 3 + 5 + … + n = ((n + 1) / 2)² — still quadratic in n.
Remember
Key Takeaways
Rule: for each odd i up to max, print digits 1..i with no spaces.
Step by 2:i += 2 is what creates odd row lengths.
Break the row: call println only after the inner loop.
Complexity:O(n²) time; O(1) extra space.
One line: for odd i, print(j) from 1 to i, then println().
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.
System.out.print(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.
System.out.print(j) stays on the same line. System.out.println() ends the current line. Digits use print; the row break uses println 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.
Use sc.hasNextInt() before sc.nextInt(), require max ≥ 1, and reject non-numeric input — see Example 2 notes.
🤔
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.