A 0-centered descending mirror prints ascending digits i..max, a fixed 0, then descending max..i — each row mirrors around zero.
Remember
Rule: for i from max+1 down to 1
print j from i to max
print 0
print k from max down to i
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321 ← max = 9
Follows the palindrome triangle in Program 27; next is the spaced mirror in Program 29.
Approach
How to Solve It
Outer loop decreases i from max+1 to 1. Print left half, then 0, then the mirror half.
Method
Idea
Best for
Three loops + fixed 0
Ascend i..max, print 0, descend max..i
Learning, interviews, exams
Spaced digits
Same loops with cout << j << " "
Easier reading for larger max
Pseudocode
Pseudocode
for i from max + 1 down to 1:
for j from i to max:
print j
print 0
for k from max down to i:
print k
print newline
Cheat sheet
Goal
Pattern
Shrink start digit
for (i = max + 1; i >= 1; i--)
Left ascending
for (j = i; j <= max; j++) cout << j;
Fixed center
cout << "0";
Right mirror
for (k = max; k >= i; k--) cout << k;
End of row
cout << "\n";
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << j / k / "0"
Stays on the same line
Each digit on the row
cout << "\n"
Ends the current line
After both half-loops and the center finish
Print digits without a newline, then end the row once.
Try it
Live Preview
Change the max digit and the 0-centered mirror updates instantly — capped at 9 for readable demos.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultmax = 5 · 36 digits
0
505
45054
3450543
234505432
12345054321
Trace
Worked Walkthrough — max = 4
Trace how left half, center 0, and right half build each row as i decreases.
i
Left
Center
Right
Prints
5
(none)
0
(none)
0
4
4
0
4
404
3
34
0
43
34043
2
234
0
432
2340432
1
1234
0
4321
123404321
There are max + 1 rows. Digit total = (max + 1)² → O(n²) for max digit n.
Code
C++ Programs
Three complete programs: fixed max = 9, cin input, and a compact max = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed max = 9
Hard-coded max digit — ascend i..9, print 0, then mirror 9..i.
C++
#include <iostream>
using namespace std;
int main()
{
int i, j, k;
for (i = 10; i >= 1; i--)
{
for (j = i; j < 10; j++)
cout << j;
cout << "0";
for (k = 9; k >= i; k--)
cout << k;
cout << "\n";
}
return 0;
}
1. Outer shrinks the start.i runs from 10 down to 1 so each row grows.
2. Left, center, right. Print i..9, then 0, then 9..i on the same line.
3. First row is just 0. When i = 10, both half-loops are empty — only the center prints.
Example 2 — User Input Max
Read max with cin, validate, then use the same three-part core.
C++
#include <iostream>
using namespace std;
int main()
{
int max;
int i, j, k;
cout << "Enter max digit (1-9): ";
if (!(cin >> max) || max < 1 || max > 9)
{
cout << "Please enter a whole number from 1 to 9.\n";
return 1;
}
for (i = max + 1; i >= 1; i--)
{
for (j = i; j <= max; j++)
cout << j;
cout << "0";
for (k = max; k >= i; k--)
cout << k;
cout << "\n";
}
return 0;
}
Output (when user enters 4)
Enter max digit (1-9): 4
0
404
34043
2340432
123404321
How It Works
1. Prompt and validate. Reject failed reads and values outside 1..9 before printing.
2. Same core. Replace hard-coded 9 / 10 with max / max + 1.
3. Safer input tip. Keep demos readable by capping at single digits:
Safer input
if (!(cin >> max) || max < 1 || max > 9)
{
cout << "Enter a whole number from 1 to 9.\n";
return 1;
}
Example 3 — Compact max = 3
Same structure with a small max — easy to confirm the empty first row and growing halves.
C++
#include <iostream>
using namespace std;
int main()
{
int max = 3;
int i, j, k;
for (i = max + 1; i >= 1; i--)
{
for (j = i; j <= max; j++)
cout << j;
cout << "0";
for (k = max; k >= i; k--)
cout << k;
cout << "\n";
}
return 0;
}
Output
0
303
23032
1230321
How It Works
1. Four rows.i = 4 → 0; i = 3 → 303; then 23032 and 1230321.
2. Trace on paper. If you forget cout << "0", the halves glue together with no center.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for max 9 or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
no 0
Missing center
Without cout << "0" the halves join with no pivot — keep the center print between both loops.
i = max
Skipped first row
Starting at i = max skips the lone 0 row. Outer loop must begin at max + 1.
\n inside
Broken rows
If cout << "\n" sits inside either half-loop, the mirror splits across lines. Call it only after the full row.
max = 1
Smallest mirror
Output is 0 then 101 — a good sanity check for bounds and the center print.
max > 9
Multi-digit glue
Values above 9 print multi-digit numbers that run together. Cap demos or add spaces.
cin
Check the stream
Validate cin >> max before looping — a failed read leaves max unset or stale.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(n²)
O(1)
Compact max = 3 (Example 3)
O(n²)
O(1)
There are n + 1 rows for max digit n. Digit total = (n + 1)² → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Three parts: print i..max, then 0, then max..i.
Start at max+1: the first row is only 0; then both halves grow.
Break the row: digits and center with no newline; cout << "\n" after the full row.
Complexity:O(n²) time from (n+1)² digits; O(1) extra space.
One line: for each i from max+1 down to 1, print i..max, then 0, then max..i, then newline.
Frequently Asked Questions
A 0-centered descending mirror: for max=9 you get 0 / 909 / 89098 / … / 1234567890987654321 — each row mirrors around a fixed zero.
cout << "0" sits between the ascending and descending loops, creating a fixed center on every row.
When i = max+1, both side loops are empty — only 0 is printed.
Starting at max+1 gives the single-0 row first; then i decreases and both halves grow toward max.
Program 27 mirrors 1..i on each row. Program 28 uses a fixed 0 center and grows digits toward max on both sides as i decreases.
Use cout << j << " " and cout << k << " " in the loops instead of cout << j.
After prompting, use if (!(cin >> max) || max < 1 || max > 9) to reject bad input before printing.
O(n²) for max digit n because there are n+1 rows and the digit total is (n+1)².
🤔
Did you know?
This pattern prints ascending digits from i to max, a fixed 0 in the center, then descending digits from max down to i. As i decreases, each row grows into the long mirror 1234567890987654321.