Shape Rule
Palindromic row
Row i prints i..(2i-1) ascending, then back down to i — always 2i-1 digits.

Program 52 prints an increasing-decreasing number pyramid: each row is palindromic — count up from i to the peak, then back down — a natural step after Program 51’s alternating number triangle. This tutorial covers two inner loops per row, peak step-back with m -= 2, a live preview, worked Python examples, edge cases, and complexity.
Palindromic row
Row i prints i..(2i-1) ascending, then back down to i — always 2i-1 digits.
i = 1..rows
for i in range(1, rows + 1): sets m = i as the starting number each row.
j = 1..i
for _ in range(i): print(m, end=""); m += 1 prints up to the peak.
m -= 2
Step back before the decreasing loop so the peak digit is not printed twice.
k = 1..(i-1)
for _ in range(i - 1): print(m, end=""); m -= 1 mirrors the ascending half.
Complexity
Total prints = 1+3+5+…+(2n-1) = n² — each row grows by 2 digits.
An increasing-decreasing number pyramid pattern prints row i as a palindrome — count up from i to the peak 2i-1, then back down to i. With rows = 5, you get 1, 232, 34543, 4567654, 567898765.
In Python, set m = i each row, print the increasing half with m += 1, step back with m -= 2, then print the decreasing half with m -= 1 before print().
It bridges Program 51’s alternating triangle to palindromic rows — combining two inner loops with a peak step-back trick.
m starts at i; print i times with m += 1.
m -= 2 skips repeating the peak digit.
Program 51 uses a continuous counter; Program 52 resets m = i and builds a palindromic row.
Follow Program 51; continue to Program 53 next.
In short: set m = i, print increasing with m += 1, step back m -= 2, print decreasing with m -= 1, then print().
Given row count rows = 5, print an increasing-decreasing number pyramid — row i shows a palindromic sequence from i up to 2i-1 and back.
# rows = 5
//1
//232
//34543
//4567654
//567898765 | Item | Type | Description |
|---|---|---|
rows | int | How many triangle rows to print. |
i (outer) | int | Current row index — runs from 1 to rows. |
m | int | Current print value — starts at i each row; incremented then decremented. |
j (increasing) | int | Prints i ascending digits with m += 1. |
k (decreasing) | int | Prints i-1 descending digits with m -= 1 after m -= 2. |
| Row length | int | Row i prints exactly 2i-1 digits. |
for i from 1 to rows:
m = i
for j from 1 to i:
print m; m += 1
m -= 2
for k from 1 to i - 1:
print m; m -= 1
print newline | Approach | Idea | Best for |
|---|---|---|
| Two inner loops | Increasing m += 1, then decreasing m -= 1 after m -= 2 | Learning and interviews |
| Peak step-back | m -= 2 skips repeating the peak digit | Palindromic row construction |
| User-input rows | int(input()) | Flexible row count |
| Compact trace | rows = 3 on paper first | Quick dry-runs before full demo |
| Spaced variant | print(m, end=" ") | Easier reading per row |
| Goal | Pattern |
|---|---|
| Outer loop | for i in range(1, rows + 1): |
| Init m per row | int m = i; |
| Increasing half | for _ in range(i): print(m, end=""); m += 1 |
| Peak step-back | m -= 2 |
| Decreasing half | for _ in range(i - 1): print(m, end=""); m -= 1 |
| End row | print() |
| Program 51 contrast | Program 51 uses a continuous counter; Program 52 builds palindromic rows with m = i |
Same triangle — three ways to set row count and format output.
rows = 5Hard-coded height for demos
int(input())Read row count from console
rows = 3Quick dry-run on paper
m -= 2Skip repeating the peak digit
2i - 1Digits per row i
Reach for this pattern when teaching palindromic sequences, two inner loops per row, and the peak step-back trick.
Natural follow-up after Program 51’s alternating triangle — introduces palindromic rows per line.
Each row reads symmetrically — good bridge to string palindrome problems.
Total prints = 1+3+5+…+(2n-1) = n² — classic nested-loop complexity.
Compare Program 51 (alternating triangle) with this palindromic pyramid, then continue to Program 53.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in palindromic rows, peak step-back, and O(n²) thinking.
Choose row count between 3 and 9 and draw the increasing-decreasing number pyramid in the browser.
Three complete Python programs — fixed rows, user input, and a compact trace demo. Click View Output to reveal sample console results.
Print five rows of the palindromic number pyramid with increasing then decreasing halves per row.
rows = 5Hard-coded row count — print ascending with m += 1, step back with m -= 2, then print descending with m -= 1.
rows = 5
for i in range(1, rows + 1):
m = i
for _ in range(i):
print(m, end="")
m += 1
m -= 2
for _ in range(i - 1):
print(m, end="")
m -= 1
print() When i = 3, m prints 345, then m -= 2 gives 3, and the second loop prints 43 — output 34543. When i = 1, only the increasing loop runs and the decreasing loop is skipped.
Read row count with int(input()) and validation.
Read rows with int(input()) and validate the result.
try:
rows = int(input("Enter the number of rows: "))
except ValueError:
print("Please enter a positive integer.")
else:
if rows <= 0:
print("Please enter a positive integer.")
else:
for i in range(1, rows + 1):
m = i
for _ in range(i):
print(m, end="")
m += 1
m -= 2
for _ in range(i - 1):
print(m, end="")
m -= 1
print() Same palindromic two-loop core as Example 1; only the source of rows changes from a literal to user input.
Smaller row count for quick tracing on paper or in interviews.
rows = 3Use rows = 3 to trace the increasing half, peak step-back, and decreasing half before scaling to 5 rows.
rows = 3
for i in range(1, rows + 1):
m = i
for _ in range(i):
print(m, end="")
m += 1
m -= 2
for _ in range(i - 1):
print(m, end="")
m -= 1
print() With only three rows you can trace every m += 1 and m -= 1 step on paper before running the full rows = 5 demo.
Before each row, m = i — the starting digit for the palindromic sequence.
for _ in range(i): print(m, end=""); m += 1 — counts up to the peak.
m -= 2 — avoids printing the peak digit twice in the decreasing half.
for _ in range(i - 1): print(m, end=""); m -= 1 then print().
Total prints = 1+3+5+…+(2n-1) = n² — O(n²) time, O(1) extra memory.
rows = 5Trace each row’s increasing half, peak step-back, decreasing half, and full line output.
i | Peak | Increasing | After m-2 | Decreasing | Row output |
|---|---|---|---|---|---|
1 | 1 | 1 | (skip) | (skip) | 1 |
2 | 3 | 23 | 2 | 2 | 232 |
3 | 5 | 345 | 3 | 43 | 34543 |
4 | 7 | 4567 | 5 | 654 | 4567654 |
5 | 9 | 56789 | 7 | 8765 | 567898765 |
Row i always prints exactly 2i-1 digits — a palindromic line built from two inner loops.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Inner bound grows with outer index — classic nested-loop exercise.
Example: trace row i = 4 in the walkthrough table.
Each row reads symmetrically — good bridge to string palindrome problems.
Example: row 5 ends with 567898765 — nine digits on a palindromic line.
Practice print(m, end="") vs print() with two inner loops per row.
Example: put print() inside the inner loop by mistake.
Total prints = n(n+1)/2 — links loops to summation formulas.
Example: 10 rows print 55 values total.
Growing inner bound makes O(n²) concrete — count prints for n rows.
Example: 5 rows = 1+2+3+4+5 = 15 prints.
Pair the pattern with int(input()) and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C courses.
Wrong inner bounds show up immediately as a broken triangle.
Each row is a palindromic sequence — not abstract loop drill.
Change rows, use fixed-width format, or switch to full rectangular table.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace row i = 3 on paper — watch m print 345, step back to 3, then print 43.
Small habits that keep number-pattern code clean.
Row i prints exactly i values — use for _ in range(i):.
Avoid crashes when the user types letters instead of a number.
Only call print() after both inner loops finish the row.
Trace rows = 3 on paper before coding the full rows = 5 demo.
Trace five rows on paper before coding the full 10-row demo.
Pro Tip: if the output is a vertical list of single numbers, you almost certainly put print() inside the inner loop.
Mistakes that commonly break increasing-decreasing number pyramid patterns.
Each number lands on its own line — you get a column, not a triangle.
→ Use print(m, end="") in both loops; print() only after both inner loops.
Using range(rows) every row makes a full rectangle, not a triangle.
→ Use for _ in range(i): — inner bound depends on outer i.
The peak digit prints twice — row looks like 2332 instead of 232.
→ Always step back with m -= 2 before the decreasing loop.
All numbers print on one long line without row breaks.
→ Add print() after both inner loops complete.
Letters or empty input raise ValueError when int(input()) is unchecked.
→ Wrap in try/except ValueError and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 on one line.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Five rows ending with 567898765 — good for dry-runs.
Unchecked int(input()) raises ValueError — use try/except.
Row 9 has 9 numbers — total prints grow as n(n+1)/2.
Try these variations to lock in the pattern.
print(m, end=" ")m = i. Increasing: for _ in range(i) with m += 1. Decreasing: for _ in range(i - 1) with m -= 1 after m -= 2.print(m, end="") stays on the line; print() advances — call it only after both inner loops finish.rows > 0 for interactive programs; rows = 1 prints a single 1.1+3+5+…+(2n-1) = n² for n rows — each row has 2i-1 digits.Quick Takeaway: set m = i, print increasing with m += 1, step back m -= 2, print decreasing with m -= 1, then print().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Total prints for n rows | n(n+1)/2 values | 2i-1 digits on row i |
The increasing-decreasing number pyramid is a natural follow-up to Program 51: palindromic rows built with two inner loops and a peak step-back. Master the fixed-rows version, then try user input and the compact 3-row trace.
Practice the three examples above, then continue to Program 53 for the next pattern in the series.
Row i prints 2i-1 palindromic digits — ascending to the peak, then back down.
int m = i at the start of each rowfor _ in range(i): print(m, end=""); m += 1m -= 2for _ in range(i - 1): print(m, end=""); m -= 1print() after both inner loopsm -= 2 — the peak prints twicerange(i) in the decreasing loop when you meant range(i - 1)print() inside either inner looprows = 3 dry-run before coding rows = 5Print the pattern the beginner-friendly way.
Palindromic: i up to 2i-1 down
Definitionm = i each row
Codem -= 2
Code2i - 1 digits
LogicO(n²) time
AnalysisEach row is palindromic: print i..(2i-1) ascending, then back down with m -= 2 to skip the peak. Row 3 prints 34543 — total digits = 1+3+5+…+(2n-1) = n² for n rows.
Move on to the next pattern in the Python number-pattern series.
12 people found this page helpful