Python 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: for i = max..1 step -2, print 1..i

1234567
12345
123
1   ← max = 7

In Python you solve it with an outer loop that steps by 2: for i in range(max_n, 0, -2), an inner loop that prints each digit with print(j, end=""), then bare print() ends the row. Program 13 flips direction with i % 2; this pattern always ascends but skips even widths.

How to Solve It

Outer loop counts down from an odd max_n with step 2. Inner loop prints 1..i. End each row with print().

MethodIdeaBest for
Step by 2Outer range(..., -2) visits only odd widthsLearning, interviews, exams
Every widthSame inner loop with range(..., -1) for comparisonSeeing what the custom step skips

Pseudocode

Pseudocode
for i from max down to 1 step 2:
    for j from 1 to i:
        print j (no newline)
    print newline

Cheat sheet

GoalPattern
Walk odd widthsfor i in range(max_n, 0, -2):
Print 1..ifor j in range(1, i + 1): print(j, end="")
End the rowprint()
Force odd maxif max_n % 2 == 0: max_n -= 1
Every width (compare)for i in range(max_n, 0, -1):

Printing Numbers vs Starting a New Line

APIEffectUse for
print(j, end="")Stays on the same lineEach digit in 1..i
print()Ends the current lineAfter the inner loop

Print all 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 so the top row matches the classic demo.

Whole numbers from 1 to 9 (single digits). Odd values keep the classic odd-only shape.

Live result max = 7 · 16 digits
1234567
12345
123
1

Worked Walkthrough

Trace max_n = 7 — each outer-loop value of i, the inner range, and the printed row.

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

Total digits are 7+5+3+1 = 16 — for odd max n, that equals ((n+1)/2)².

Python Programs

Three complete programs: fixed odd max, input(), and an every-width comparison. Use View Output for sample results.

Example 1 — Fixed max_n = 7

Hard-coded odd maximum — outer steps by 2, inner prints 1..i.

Python
max_n = 7

for i in range(max_n, 0, -2):
    for j in range(1, i + 1):
        print(j, end="")
    print()

How It Works

1. Outer skips even widths. range(max_n, 0, -2) visits 7, 5, 3, 1 only.

2. Inner prints ascending digits. j runs from 1 to i with print(j, end="").

3. End the row. Call bare print() only after the inner loop finishes.

Example 2 — User Input

Read the maximum width from the keyboard. Prefer an odd value so the top row matches the classic demo.

Python
max_n = int(input("Enter an odd maximum (e.g. 9): "))

for i in range(max_n, 0, -2):
    for j in range(1, i + 1):
        print(j, end="")
    print()

How It Works

1. Same loop core. Only the source of max_n changes from a literal to keyboard input.

2. Entering 5 stops early. You get three odd-length rows ending at 1.

3. Safer input tip. Validate and force an odd top width:

Safer input
raw = input("Enter an odd maximum (e.g. 9): ").strip()
try:
    max_n = int(raw)
except ValueError:
    print("Enter a positive whole number.")
    raise SystemExit(1)
if max_n < 1:
    print("Enter a positive whole number.")
    raise SystemExit(1)
if max_n % 2 == 0:
    max_n -= 1

Example 3 — Every Width (range(..., -1))

Same inner loop with a normal step — includes even lengths for comparison.

Python
max_n = 7

for i in range(max_n, 0, -1):
    for j in range(1, i + 1):
        print(j, end="")
    print()

How It Works

1. Only the step changed. range(..., -1) visits every width; range(..., -2) in Example 1 skips the even ones.

2. Same inner rule. Digits still print 1..i with print(j, end="").

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_n % 2 == 0: max_n -= 1.

wrong step

Use range(..., -2), not -1

range(..., -1) reprints the every-width triangle (see Example 3).

print(i)

print(i, end="") instead of print(j, end="")

That repeats the row length. This pattern prints the sequence value of j.

print() inside

Bare print(j) inside the inner loop

That puts each digit on its own line. Use print(j, end="") for digits; call bare print() only after the row finishes.

max = 1

Single digit

Output is just 1 — a good sanity check.

max ≤ 0

Empty output

The outer loop never runs. Validate and prompt again for clearer UX.

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 you print 1+3+…+n digits — about half of the full triangle, but still O(n²).

Key Takeaways

  • Rule: for odd i from max_n down by 2, print digits 1..i.
  • Custom step: range(..., -2) is what skips even row lengths.
  • end= vs print(): digits stay on the line; bare print() advances after each row.
  • Next step: Program 15 builds an alternating binary triangle with 0 and 1.

One line: step i down by 2 from an odd max, print 1..i with end="", then call print().

Frequently Asked Questions

The outer loop uses range(max_n, 0, -2), so it visits only odd widths: 7, 5, 3, 1. Even lengths like 6, 4, 2 are skipped.
Because the step of -2 skips even row lengths. After 1234567 (7 digits), the next row is 5 digits (12345), not 6.
print(j, end="") stays on the same line. print() ends the current line. Digits use end=""; the row break uses print() after the inner loop.
Program 13 alternates ascending/descending direction with i % 2 (12345, 4321, 123, 21, 1). Program 14 always prints 1..i but only for odd row lengths using a step of -2.
O(n²) where n is the maximum row width. You print about 1+3+5+…+n odd-width digits, which is still O(n²).
Subtract 1 to make it odd (e.g. if max_n % 2 == 0: max_n -= 1) so the first row stays odd-length, or document that even input shifts the pattern.
Wrap int(input()) in try/except ValueError, or check that the raw string is digits, and require max_n >= 1.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

Did you know?

Only odd-length rows print. The outer loop uses range(..., -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 rows built from alternating 0 and 1.

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