Java Odd Number Triangle Pattern (Left-Shifted)

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

What Is This Pattern?

A left-shifted odd number triangle prints only odd digits from the row start through max, stepping by 2 — so you see 13579, 3579, 579, and so on.

Remember
Rule: outer i = 1..max step 2; inner j = i..max step 2

13579
3579
579
79
9          ← max = 10

Same left-shift idea as Program 2, but both loops use += 2 so even numbers never appear. Next, Program 18 alternates odd and even rows.

How to Solve It

Outer loop from 1 to max stepping by 2. Inner loop from current i to max stepping by 2, printing each value. End the line after the inner loop.

MethodIdeaBest for
Odd stepsBoth loops use += 2 from an odd startLearning custom loop steps
Even mirrorStart both loops at 2Comparing odd-only vs even-only

Pseudocode

Pseudocode
for i from 1 to max step 2:
    for j from i to max step 2:
        print j
    print newline

Cheat sheet

GoalPattern
Outer (row start)for (int i = 1; i <= max; i += 2)
Inner (odds to max)for (int j = i; j <= max; j += 2) System.out.print(j);
End the rowSystem.out.println();
Force odd maxif (max % 2 == 0) max -= 1;
Even-only mirrorStart both loops at 2 with += 2
Spaced digitsSystem.out.print(j + " ");

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach odd digit j
System.out.printlnEnds the current lineAfter the inner loop

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

Live Preview

Change the maximum and the left-shifted odd triangle updates instantly.

Whole numbers from 1 to 20. Odd steps never print even values — tap a chip or type a value.

Live result max = 10 · 15 digits
13579
3579
579
79
9

Worked Walkthrough

Trace three outer values when max = 10 — watch the start shift right while only odds print.

Outer iInner jPrints
11, 3, 5, 7, 913579
55, 7, 9579
999

10 never prints because the loops stay on odd values. Raising i by 2 drops one odd digit from the left each row.

Java Programs

Three complete programs: fixed max = 10, Scanner input with even adjustment, and an even-number mirror. Use View Output to reveal sample results.

Example 1 — Fixed max = 10

Hard-coded maximum — both loops step by 2 from an odd start.

Java
public class LeftShiftedOddTriangle {
    public static void main(String[] args) {
        int max = 10;

        for (int i = 1; i <= max; i += 2) {
            for (int j = i; j <= max; j += 2) {
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

How It Works

1. Outer picks the start. i runs 1, 3, 5, 7, 9 — that value is where each row begins.

2. Inner prints odds to max. j steps from i to max by 2 with System.out.print(j).

3. End the row. Call System.out.println() only after the inner loop finishes.

Example 2 — User Input Max

Read the maximum at runtime. Subtract 1 when the value is even so the bound is odd.

Java
import java.util.Scanner;

public class LeftShiftedOddTriangleInput {
    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 = i; j <= max; j += 2) {
                System.out.print(j);
            }
            System.out.println();
        }
        sc.close();
    }
}

How It Works

1. Adjust even input. 8 becomes 7 so the last odd matches a clear odd bound.

2. Same loop core. The nested += 2 loops match Example 1.

3. Validate in real apps. Prefer checking hasNextInt() and requiring a positive maximum (tip below).

Safer input tip
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;
}
if (max % 2 == 0) max -= 1;

Example 3 — Even Number Mirror

Start both loops at 2 to print only even digits with the same left-shift shape.

Java
public class LeftShiftedEvenTriangle {
    public static void main(String[] args) {
        int max = 10;

        for (int i = 2; i <= max; i += 2) {
            for (int j = i; j <= max; j += 2) {
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

How It Works

1. Same left-shift structure. Only the start value changes from 1 to 2.

2. Even values only. i and j visit 2, 4, 6, 8, 10.

3. Compare side by side. Seeing both odd and even mirrors makes the += 2 step easy to remember.

Edge Cases & Pitfalls

Check these before calling the solution done.

Wrong step

Use += 2, not ++

Using j++ reprints consecutive integers like Program 2, not an odd-only triangle.

Even max

Optional: force an odd bound

if (max % 2 == 0) max -= 1; keeps the last odd clear when reading input.

println inside

Do not put println inside the inner loop

That prints one digit per line and destroys the triangle.

Bad input

Validate with hasNextInt

nextInt() throws on letters — prefer hasNextInt() and require max >= 1.

Time and Space Complexity

ProgramTimeExtra space
Odd triangle (Examples 1–2)O(n²)O(1)
Even mirror (Example 3)O(n²)O(1)

About half the integers up to n are visited, but nested loops still give roughly k(k + 1) / 2 prints where k ≈ n / 2 — quadratic in n. Extra memory is only the loop variables.

Key Takeaways

  • Rule: outer i = 1..max step 2; inner j = i..max step 2.
  • Shift: each row starts at a larger odd value, so the left edge moves right.
  • Break the row: call println only after the inner loop finishes.
  • Complexity: O(n²) time, O(1) extra space.

One line: start at an odd i, print odds through max with += 2, then raise the start.

Frequently Asked Questions

A left-shifted odd number triangle: for max=10 you get 13579, 3579, 579, 79, 9.
Both loops increment by 2 (i += 2 and j += 2), so they visit only odd values: 1, 3, 5, 7, 9.
Each next row starts at a higher odd i, so numbers below i are not printed anymore — the triangle shortens from the left.
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.
Subtract 1 (max -= 1) so the bound is odd, as shown in Example 2. Without that, odd steps still never print the even max.
Program 2 prints consecutive integers i..rows. Program 17 prints only odds from i to max with step 2.
O(n²) for maximum value n. Only about half the numbers are visited due to step size 2, but nested loops still dominate.
One row prints a single digit 1.

Did you know?

Both loops step by 2 with i += 2 and j += 2, so only odd numbers print. Each row starts at a larger odd value, so the triangle shifts left — still O(n²) for maximum n.

Next: Alternating Odd/Even Triangle

Continue with the next pattern in the Java number-pattern series.

Program 18 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