Java Number Rows Pattern (Odd Length)

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

What Is This Pattern?

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).

How to Solve It

Step the outer loop by 2; print 1..i with System.out.print(j).

MethodIdeaBest for
Odd lengthsOuter i += 2; inner prints 1..iLearning, interviews, exams
Odd digitsSame outer loop; inner uses j += 2Rows 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

GoalPattern
Walk odd lengthsfor (int i = 1; i <= max; i += 2)
Print 1..ifor (int j = 1; j <= i; j++) System.out.print(j);
No spacesUse print(j), not print(j + " ")
End the rowSystem.out.println();
Even max fixif (max % 2 == 0) max -= 1;
Odd digits onlyfor (int j = 1; j <= i; j += 2)

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach digit j (no space)
System.out.printlnEnds 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 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 result max = 9 · 5 rows · 25 digits
1
123
12345
1234567
123456789

Worked Walkthrough — max = 7

Trace each odd i, the digit range, and the printed line.

iInner rangeDigitsPrinted row
11..111
31..33123
51..5512345
71..771234567

Total digits: 1 + 3 + 5 + 7 = 16. In general, for odd max n, the sum is ((n + 1) / 2)² — still O(n²).

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();
        }
    }
}

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();
    }
}

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();
        }
    }
}

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.

Time and Space Complexity

ProgramTimeExtra 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.

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.

Next: Number & Asterisk Mirror

Move on to the number and asterisk mirror pattern in the Java 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