Unlike Program 1 (i--, every width) or Program 13 (direction flips with parity), this pattern keeps ascending digits and skips even lengths with i -= 2.
Approach
How to Solve It
Outer loop from max down to 1, stepping by 2. Inner loop from 1 to current i, printing each digit. End the line after the inner loop.
Method
Idea
Best for
Step by 2
Outer i -= 2, print 1..i
Learning custom loop steps
Every width
Same inner loop with i--
Comparing odd-only vs full triangle
Pseudocode
Pseudocode
for i from max down to 1 step -2:
for j from 1 to i:
print j
print newline
Cheat sheet
Goal
Pattern
Outer (odd widths)
for (int i = max; i >= 1; i -= 2)
Inner (print 1..i)
for (int j = 1; j <= i; j++) System.out.print(j);
End the row
System.out.println();
Force odd max
if (max % 2 == 0) max--;
Every width
Use i-- instead of i -= 2
Digit count (odd max)
((max + 1) / 2)²
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each digit j
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 width and the odd-length triangle updates instantly.
Prefer odd values from 1 to 19. Tap a chip or type a value — the preview redraws as you go.
Live resultmax = 7 · 16 digits
1234567
12345
123
1
Trace
Worked Walkthrough
Trace three outer values when max = 7 — watch even widths disappear because of i -= 2.
Outer i
Inner j
Prints
7
1..7
1234567
5
1..5
12345
1
1..1
1
After i = 7, the next value is 5 — not 6. That single step change is the whole pattern.
Code
Java Programs
Three complete programs: fixed max = 7, Scanner input, and an every-width comparison with i--. Use View Output to reveal sample results.
Example 1 — Fixed max = 7
Hard-coded odd maximum — outer steps by 2, inner prints 1..i.
Java
public class OddLengthDescendingTriangle {
public static void main(String[] args) {
int max = 7;
for (int i = max; i >= 1; i -= 2) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
2. Inner prints ascending digits.j runs from 1 to i 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 width at runtime. Prefer an odd value so the top row matches the classic demo.
Java
import java.util.Scanner;
public class OddLengthDescendingTriangleInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter an odd maximum (e.g. 9): ");
int max = sc.nextInt();
for (int i = max; i >= 1; i -= 2) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
sc.close();
}
}
Output (when user enters 5)
Enter an odd maximum (e.g. 9): 5
12345
123
1
How It Works
1. Same loop core. Only the source of max changes from a literal to Scanner.
2. Entering 5 stops early. You get three odd-length rows ending at 1.
3. Force odd in real apps. Prefer checking input and forcing an odd top width (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--;
Example 3 — Every Width (i--)
Same inner loop with a normal step — includes even lengths for comparison.
Java
public class EveryWidthDescendingTriangle {
public static void main(String[] args) {
int max = 7;
for (int i = max; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
Output
1234567
123456
12345
1234
123
12
1
How It Works
1. Only the step changed.i-- visits every width; i -= 2 in Example 1 skips the even ones.
2. Same inner rule. Digits still print 1..i with System.out.print(j).
3. Compare side by side. Seeing both outputs makes the custom step easy to remember.
Edge Cases & Pitfalls
Check these before calling the solution done.
Even max
Start with an odd maximum
An even start prints even lengths (6, 4, 2). Force odd with if (max % 2 == 0) max--;.
Wrong step
Use i -= 2, not i--
i-- reprints Program 1’s every-width triangle (see Example 3).
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 widths (Examples 1–2)
O(n²)
O(1)
Every width (Example 3)
O(n²)
O(1)
For odd max = n, digits printed are n + (n − 2) + … + 1 = ((n + 1) / 2)² — still quadratic in n. Extra memory is only the loop variables.
Remember
Key Takeaways
Rule: outer i = max..1 with i -= 2; inner print 1..i.
Skip: even widths never run — that is the only new idea vs Program 1.
Break the row: call println only after the inner loop finishes.
Complexity:O(n²) time, O(1) extra space.
One line: print 1..i while i counts down by twos from an odd maximum.
Frequently Asked Questions
An odd-length descending number triangle: for max=7 you get 1234567, 12345, 123, 1.
The outer loop uses i -= 2, so it visits only odd widths: 7, 5, 3, 1. Even lengths like 6, 4, 2 are skipped.
Because i -= 2 skips even row lengths. After 1234567 (7 digits), the next row is 5 digits (12345), not 6.
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.
Program 13 alternates direction with i % 2 (12345, 4321…). Program 14 always prints 1..i but only for odd lengths using i -= 2.
Change the outer step to i--. Then you print 7, 6, 5, 4, 3, 2, 1 — see Example 3.
O(n²) for maximum width n. Odd lengths alone still print about n²/4 digits.
Starting at an even max prints even lengths (6, 4, 2…). Prefer an odd max, or force odd with if (max % 2 == 0) max--;
🤔
Did you know?
Only odd-length rows print. The outer loop uses i −= 2 (7, 5, 3, 1) and the inner loop prints 1..i — still O(n²) total digit prints for maximum width n.