Print digits without a newline, then end the row once.
Try it
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 resultmax = 10 · 15 digits
13579
3579
579
79
9
Trace
Worked Walkthrough
Trace three outer values when max = 10 — watch the start shift right while only odds print.
Outer i
Inner j
Prints
1
1, 3, 5, 7, 9
13579
5
5, 7, 9
579
9
9
9
10 never prints because the loops stay on odd values. Raising i by 2 drops one odd digit from the left each row.
Code
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();
}
}
}
Output
13579
3579
579
79
9
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();
}
}
Output (when user enters 8)
Enter the maximum value: 8
1357
357
57
7
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();
}
}
}
Output
246810
46810
6810
810
10
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
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.