A diagonal mirror number diamond is Program 57’s inverse-V pyramid plus a mirrored bottom half. Each row still prints the digit twice on left and right diagonals.
Unlike Program 57 (top half only), this page adds for (i = rows - 1; i >= 1; i--) so the shape closes into a diamond.
Approach
How to Solve It
Reuse the same left/right diagonal logic twice: once counting up to rows, then counting down from rows - 1.
Method
Idea
Best for
Two outer loops
Top half + mirrored bottom
Learning, interviews, demos
Compact trace
Same logic with rows = 3
Quick dry-runs on paper
Pseudocode
Pseudocode
print_row(i):
for j from rows down to 1:
if i == j: print j else print space
for k from 2 to rows:
if i == k: print k else print space
print newline
for i from 1 to rows:
print_row(i)
for i from (rows - 1) down to 1:
print_row(i)
Cheat sheet
Goal
Pattern
Top half
for (i = 1; i <= rows; i++)
Bottom half
for (i = rows - 1; i >= 1; i--)
Left diagonal
for (j = rows; j >= 1; j--) then if (i == j)
Right diagonal
for (k = 2; k <= rows; k++) then if (i == k)
End the row
cout << "\n";
Total lines
2 * rows - 1
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << j / cout << " "
Stays on the same line
Each column slot
cout << "\n"
Ends the line
After both inner loops
Print digits and spaces without a newline, then end the row once.
Try it
Live Preview
Change the half-height and the hollow number diamond updates instantly.
Use half-height from 1 to 9. Total lines = 2×rows−1. Tap a chip or type a value — the preview redraws as you go.
Live result5 half · 9 lines
1
2 2
3 3
4 4
5 5
4 4
3 3
2 2
1
Trace
Worked Walkthrough
Trace how rows = 5 builds nine lines — top half, then bottom half without repeating the peak.
Phase
Outer i
Lines printed
Top
1..5
1 … 5 5
Bottom
4..1
4 4 … 1
Skip
Peak not repeated
bottom starts at rows-1
Inner loops are identical in both halves — only the outer direction changes.
Code
C++ Programs
Three complete programs: fixed rows = 5, cin input, and a compact rows = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded half-height — top 1..rows, then bottom rows-1..1 with the same diagonal scans.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 5;
int i, j, k;
for (i = 1; i <= rows; i++)
{
for (j = rows; j >= 1; j--)
{
if (i == j)
cout << j;
else
cout << " ";
}
for (k = 2; k <= rows; k++)
{
if (i == k)
cout << k;
else
cout << " ";
}
cout << "\n";
}
for (i = rows - 1; i >= 1; i--)
{
for (j = rows; j >= 1; j--)
{
if (i == j)
cout << j;
else
cout << " ";
}
for (k = 2; k <= rows; k++)
{
if (i == k)
cout << k;
else
cout << " ";
}
cout << "\n";
}
return 0;
}
Output
1
2 2
3 3
4 4
5 5
4 4
3 3
2 2
1
How It Works
1. Top half. Same as Program 57: left j = rows..1, right k = 2..rows, digit when equal to i.
2. Bottom half. Replay the same inner loops while i counts from rows - 1 down to 1.
3. No double peak. Skipping i = rows on the way down keeps the middle line once.
Example 2 — User Input Rows
Read the half-height at runtime with a simple validation tip.
C++
#include <iostream>
using namespace std;
int main()
{
int rows, i, j, k;
cout << "Enter the number of rows: ";
cin >> rows;
for (i = 1; i <= rows; i++)
{
for (j = rows; j >= 1; j--)
{
if (i == j)
cout << j;
else
cout << " ";
}
for (k = 2; k <= rows; k++)
{
if (i == k)
cout << k;
else
cout << " ";
}
cout << "\n";
}
for (i = rows - 1; i >= 1; i--)
{
for (j = rows; j >= 1; j--)
{
if (i == j)
cout << j;
else
cout << " ";
}
for (k = 2; k <= rows; k++)
{
if (i == k)
cout << k;
else
cout << " ";
}
cout << "\n";
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
1
2 2
3 3
4 4
3 3
2 2
1
How It Works
1. Same diamond core. Only the source of rows changes from a literal to cin.
2. Entering 4 stops early. You get seven lines (2×4−1) peaking at 4 4.
3. Validate in real apps. Prefer checking cin failure and requiring a positive height (tip below).
Safer input tip
if (!(cin >> rows) || rows < 1)
{
cout << "Please enter a positive integer.\n";
return 1;
}
Example 3 — Compact rows = 3
Five-line diamond — quick to trace both outer loops on paper.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 3;
int i, j, k;
for (i = 1; i <= rows; i++)
{
for (j = rows; j >= 1; j--)
{
if (i == j)
cout << j;
else
cout << " ";
}
for (k = 2; k <= rows; k++)
{
if (i == k)
cout << k;
else
cout << " ";
}
cout << "\n";
}
for (i = rows - 1; i >= 1; i--)
{
for (j = rows; j >= 1; j--)
{
if (i == j)
cout << j;
else
cout << " ";
}
for (k = 2; k <= rows; k++)
{
if (i == k)
cout << k;
else
cout << " ";
}
cout << "\n";
}
return 0;
}
Output
1
2 2
3 3
2 2
1
How It Works
1. Only five lines. Easy to dry-run top 1..3 and bottom 2..1 on paper.
2. Same formula. Nothing changes except rows — proving the diamond scales.
3. Symmetry check. Lines 2 and 4 both show 2 2 — mirror of the top half.
Edge Cases & Pitfalls
Check these before calling the solution done.
Double peak
Start the bottom at rows - 1
Starting at rows prints the widest line twice and breaks the diamond.
Triple digit
Keep the right loop at k = 2
Starting at 1 can reprint the center column on some rows.
Newlines
Do not put "\n" inside an inner loop
That turns the diamond into a vertical list of single characters.
Bad cin
Validate input
Check cin >> rows and require rows >= 1 before the loops.
Analysis
Time and Space Complexity
Program
Time
Extra space
Diamond (Examples 1–3)
O(n²)
O(1)
You print 2n - 1 lines, each scanning about 2n - 1 character slots — still quadratic in n. Extra memory is only a few loop variables.
Remember
Key Takeaways
Rule: top 1..rows, bottom rows-1..1, same diagonal scans.
Reuse: Program 57’s inner loops are enough — only add a second outer loop.
Break the row: call cout << "\n" only after both scans finish.
Complexity:O(n²) time, O(1) extra space.
One line: print the Program 57 pyramid, then mirror it from rows-1 down to 1.
Frequently Asked Questions
Each row prints the same digit on the left diagonal and the right diagonal; spaces fill the remaining positions.
The top half already printed the peak row. Starting at rows-1 avoids duplicating the middle line.
An inverse-V pyramid from 1 to rows, then mirrored back down to 1 — 2*rows-1 lines total.
Program 57 prints only the top pyramid half. Program 58 adds a second outer loop from rows-1 down to 1 for the bottom half.
Each column position gets either the digit or a space. Equality picks exactly the two diagonal slots for that row.
Printing a digit or space stays on the same line. Printing a newline ends the current row. Digits/spaces use cout without a newline; the row break uses cout << "\n" after both inner loops.
O(n²) for n rows because you print 2n-1 lines, each scanning about 2n positions.
Only one line prints — the bottom loop (rows-1..1) does not run.
🤔
Did you know?
Print the Program 57 pyramid for the top half, then mirror with for (i = rows-1; i >= 1; i--). Total lines = 2×rows-1 — each row scans about 2×rows-1 positions.