Java Descending Number Triangle Pattern (Odd Length)

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

What Is This Pattern?

An odd-length descending number triangle prints ascending digits 1..i, but only for odd row widths — so you see 1234567, 12345, 123, 1.

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

1234567
12345
123
1          ← max = 7

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.

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.

MethodIdeaBest for
Step by 2Outer i -= 2, print 1..iLearning custom loop steps
Every widthSame 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

GoalPattern
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 rowSystem.out.println();
Force odd maxif (max % 2 == 0) max--;
Every widthUse i-- instead of i -= 2
Digit count (odd max)((max + 1) / 2)²

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach 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 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 result max = 7 · 16 digits
1234567
12345
123
1

Worked Walkthrough

Trace three outer values when max = 7 — watch even widths disappear because of i -= 2.

Outer iInner jPrints
71..71234567
51..512345
11..11

After i = 7, the next value is 5 — not 6. That single step change is the whole pattern.

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

How It Works

1. Outer skips even widths. i -= 2 visits 7, 5, 3, 1 only.

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

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

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.

Time and Space Complexity

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

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.

Next: Alternating Binary Triangle

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

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