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 JavaScript 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 (let i = 1; i <= rows; i++) sets m = i as the starting number each row.
j = 1..i
for (let j = 0; j < i; j++) { line += m; m++; } appends 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 (let k = 0; k < i - 1; k++) { line += m; m--; } 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 JavaScript, set m = i each row, append the increasing half with m++, step back with m -= 2, then append the decreasing half with m-- before console.log(line).
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, append increasing with m++, step back m -= 2, append decreasing with m--, then console.log(line).
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 | number | How many triangle rows to print. |
i (outer) | number | Current row index — runs from 1 to rows. |
m | number | Current print value — starts at i each row; incremented then decremented. |
j (increasing) | number | Prints i ascending digits with m += 1. |
k (decreasing) | number | Prints i-1 descending digits with m -= 1 after m -= 2. |
| Row length | number | Row i prints exactly 2i-1 digits. |
for (let i = 1; i <= rows; i++) {
let m = i;
let line = "";
for (let j = 0; j < i; j++) { line += m; m++; }
m -= 2;
for (let k = 0; k < i - 1; k++) { line += m; m--; }
console.log(line);
} | 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 | parseInt(prompt()) | Flexible row count |
| Compact trace | rows = 3 on paper first | Quick dry-runs before full demo |
| Spaced variant | line += m + " " | Easier reading per row |
| Goal | Pattern |
|---|---|
| Outer loop | for (let i = 1; i <= rows; i++) |
| Init m per row | let m = i; |
| Increasing half | for (let j = 0; j < i; j++) { line += m; m++; } |
| Peak step-back | m -= 2 |
| Decreasing half | for (let k = 0; k < i - 1; k++) { line += m; m--; } |
| End row | console.log(line); |
| 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
parseInt(prompt())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 JavaScript programs — fixed rows, user input, and a compact trace demo. Click View Output to reveal sample console results, or Try it Yourself to run in the browser.
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.
const rows = 5;
for (let i = 1; i <= rows; i++) {
let m = i;
let line = "";
for (let j = 0; j < i; j++) {
line += m;
m++;
}
m -= 2;
for (let k = 0; k < i - 1; k++) {
line += m;
m--;
}
console.log(line);
} 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 prompt() and Number.isFinite validation.
Read rows with prompt() and validate the result.
const rowsInput = prompt("Enter the number of rows:");
const rows = parseInt(rowsInput, 10);
if (!Number.isFinite(rows) || rows < 1) {
console.log("Please enter a positive integer.");
} else {
for (let i = 1; i <= rows; i++) {
let m = i;
let line = "";
for (let j = 0; j < i; j++) {
line += m;
m++;
}
m -= 2;
for (let k = 0; k < i - 1; k++) {
line += m;
m--;
}
console.log(line);
}
} 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.
const rows = 3;
for (let i = 1; i <= rows; i++) {
let m = i;
let line = "";
for (let j = 0; j < i; j++) {
line += m;
m++;
}
m -= 2;
for (let k = 0; k < i - 1; k++) {
line += m;
m--;
}
console.log(line);
} 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 (let j = 0; j < i; j++) { line += m; m++; } — counts up to the peak.
m -= 2 — avoids printing the peak digit twice in the decreasing half.
for (let k = 0; k < i - 1; k++) { line += m; m--; } then console.log(line).
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 line += m vs console.log(line) with two inner loops per row.
Example: put console.log(line) inside the inner loop by mistake.
Total logs = 1+3+5+…+(2n-1) = n² — odd-count summation per row.
Example: 10 rows log 100 digits total.
Growing inner bound makes O(n²) concrete — count prints for n rows.
Example: 5 rows = 1+3+5+7+9 = 25 digit logs.
Pair the pattern with Number.isFinite and positive-row checks after parseInt(prompt()).
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 logs exactly 2i - 1 digits — use for (let j = 0; j < i; j++) then for (let k = 0; k < i - 1; k++).
Number.isFiniteAvoid crashes when the user types letters instead of a number.
Only call console.log(line) 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 console.log(line) 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 line += m in both loops; console.log(line) only after both inner loops.
Using j < rows every row makes a full rectangle, not a palindromic pyramid row.
→ Use for (let j = 0; j < i; j++) — 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 console.log(line) after both inner loops complete.
parseInt(prompt())Letters or empty input yield NaN when parseInt(prompt()) is unchecked.
→ Check Number.isFinite(rows) 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 parseInt(prompt()) returns NaN — validate with Number.isFinite.
Row 9 has 17 digits — total logs grow as n².
Try these variations to lock in the pattern.
line += m + " "m = i. Increasing: for (let j = 0; j < i; j++) with m++. Decreasing: for (let k = 0; k < i - 1; k++) with m-- after m -= 2.line += m builds the row; console.log(line) 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, append increasing with m++, step back m -= 2, append decreasing with m--, then console.log(line).
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Total digit logs for n rows | n² | O(1) |
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 (let j = 0; j < i; j++) { line += m; m++; }m -= 2for (let k = 0; k < i - 1; k++) { line += m; m--; }console.log(line) after both inner loopsm -= 2 — the peak prints twicej < i in the decreasing loop when you meant k < i - 1console.log(line) 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: append i..(2i-1) ascending, then back down with m -= 2 to skip the peak. Row 3 logs 34543 — total digits = 1+3+5+…+(2n-1) = n² for n rows.
Move on to the next pattern in the JavaScript number-pattern series.
12 people found this page helpful