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 = m - 2, a live preview, worked C# 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 = 1; i <= rows; i++) sets m = i as the starting number each row.
j = 1..i
for (j = 1; j <= i; j++) Console.Write(m++); prints up to the peak.
m = m - 2
Step back before the decreasing loop so the peak digit is not printed twice.
k = 1..(i-1)
for (k = 1; k < i; k++) Console.Write(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 C# set m = i each row, print the increasing half with m++, step back with m = m - 2, then print the decreasing half with m-- before WriteLine().
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++.
m = 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++, step back m = m - 2, print decreasing with m--, then WriteLine().
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++. |
k (decreasing) | int | Prints i-1 descending digits with m-- after m = 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++
m = m - 2
for k from 1 to i - 1:
print m; m--
print newline | Approach | Idea | Best for |
|---|---|---|
| Two inner loops | Increasing m++, then decreasing m-- after m = m - 2 | Learning and interviews |
| Peak step-back | m = m - 2 skips repeating the peak digit | Palindromic row construction |
| User-input rows | int.TryParse(...) | Flexible row count |
| Compact trace | rows = 3 on paper first | Quick dry-runs before full demo |
| Spaced variant | Console.Write(m++ + " ") | Easier reading per row |
| Goal | Pattern |
|---|---|
| Outer loop | for (i = 1; i <= rows; i++) |
| Init m per row | int m = i; |
| Increasing half | for (j = 1; j <= i; j++) Console.Write(m++); |
| Peak step-back | m = m - 2; |
| Decreasing half | for (k = 1; k < i; k++) Console.Write(m--); |
| End row | Console.WriteLine(); |
| 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
TryParseRead row count from console
rows = 3Quick dry-run on paper
m = 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 C# 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++, step back with m = m - 2, then print descending with m--.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
for (int i = 1; i <= rows; i++)
{
int m = i;
for (int j = 1; j <= i; j++)
Console.Write(m++);
m = m - 2;
for (int k = 1; k < i; k++)
Console.Write(m--);
Console.WriteLine();
}
}
}
} When i = 3, m prints 345, then m = 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 from the console with safe parsing.
Read rows from the console with int.TryParse — reject invalid input gracefully.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the number of rows: ");
if (!int.TryParse(Console.ReadLine(), out int rows) || rows <= 0)
{
Console.WriteLine("Please enter a positive integer.");
return;
}
for (int i = 1; i <= rows; i++)
{
int m = i;
for (int j = 1; j <= i; j++)
Console.Write(m++);
m = m - 2;
for (int k = 1; k < i; k++)
Console.Write(m--);
Console.WriteLine();
}
}
}
} 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.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 3;
for (int i = 1; i <= rows; i++)
{
int m = i;
for (int j = 1; j <= i; j++)
Console.Write(m++);
m = m - 2;
for (int k = 1; k < i; k++)
Console.Write(m--);
Console.WriteLine();
}
}
}
} With only three rows you can trace every m++ and m-- step on paper before running the full rows = 5 demo.
Before each row, m = i — the starting digit for the palindromic sequence.
for (j = 1; j <= i; j++) Console.Write(m++); — counts up to the peak.
m = m - 2 — avoids printing the peak digit twice in the decreasing half.
for (k = 1; k < i; k++) Console.Write(m--); then WriteLine().
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 Write vs WriteLine with multiple values per row.
Example: put WriteLine 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 TryParse 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 j <= i.
Avoid crashes when the user types letters instead of a number.
Only call WriteLine() after the inner loop finishes 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 WriteLine 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 Console.Write(m++) and Console.Write(m--); WriteLine only after both inner loops.
Using j <= rows every row makes a full rectangle, not a triangle.
→ Use for (j = 1; 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 = m - 2 before the decreasing loop.
All numbers print on one long line without row breaks.
→ Add Console.WriteLine() after each inner loop completes.
Letters or empty input throw FormatException.
→ Prefer int.TryParse 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.
Convert.ToInt32 throws — use TryParse.
Row 9 has 9 numbers — total prints grow as n(n+1)/2.
Try these variations to lock in the pattern.
Console.Write(m++ + " ")m = i. Increasing: j = 1..i with m++. Decreasing: k = 1..(i-1) with m-- after m = m - 2.Console.Write stays on the line; WriteLine advances — call it only after the inner loop finishes.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++, step back m = m - 2, print decreasing with m--, then WriteLine().
| 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 (j = 1; j <= i; j++) Console.Write(m++);m = m - 2;for (k = 1; k < i; k++) Console.Write(m--);WriteLine() after both inner loopsm = m - 2 — the peak prints twicek <= i in the decreasing loop when you meant k < iWriteLine 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 = m - 2
Code2i - 1 digits
LogicO(n²) time
AnalysisEach row is palindromic: print i..(2i-1) ascending, then back down with m = 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 C# number-pattern series.
12 people found this page helpful