A diagonal mirror number pyramid prints the row digit twice — once on the left diagonal and once on the right — with spaces everywhere else. The shape looks like an inverse V of numbers.
Remember
Rule: left j = rows..1 (digit if i==j); right k = 2..rows (digit if i==k)
1
2 2
3 3
4 4
5 5 ← rows = 5
Unlike Program 56 (full palindromic rows), this pattern prints only two digits per row and fills the rest with spaces.
Approach
How to Solve It
Outer loop over rows. For each row: scan left columns downward, then right columns from 2 — print the digit when the column equals the row, otherwise a space.
Method
Idea
Best for
Two scans
Left diagonal + right diagonal
Learning, interviews, demos
Compact trace
Same logic with rows = 3
Quick dry-runs on paper
Pseudocode
Pseudocode
for i from 1 to rows:
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
Cheat sheet
Goal
Pattern
Outer row loop
for (i = 1; i <= rows; 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)
Digit or space
cout << j; / cout << " ";
End the row
cout << "\n";
Width per row
2 * rows - 1 character slots
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 row count and the inverse-V diagonal pattern updates instantly.
Use rows from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 9 cols
1
2 2
3 3
4 4
5 5
Trace
Worked Walkthrough
Trace row i = 3 when rows = 5 — left scan, then right scan.
Step
Loop
Prints
1
Left: j = 5..1
3 (digit at j = 3)
2
Right: k = 2..5
3 (digit at k = 3)
Result
Full row 3
3 3
Starting the right loop at k = 2 skips the center so the digit is not printed three times.
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 height — left diagonal j = rows..1, then right diagonal k = 2..rows.
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";
}
return 0;
}
Output
1
2 2
3 3
4 4
5 5
How It Works
1. Left diagonal. Scan j from rows down to 1. Print j when i == j, else a space.
2. Right diagonal. Scan k from 2 to rows. Print k when i == k, else a space.
3. End the row. Call cout << "\n" only after both scans finish.
Example 2 — User Input Rows
Read the row count 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";
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
1
2 2
3 3
4 4
How It Works
1. Same diagonal core. Only the source of rows changes from a literal to cin.
2. Entering 4 stops early. You get four inverse-V rows ending 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
Smaller height for quick tracing of both diagonal conditions.
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";
}
return 0;
}
Output
1
2 2
3 3
How It Works
1. Only three rows. Easy to dry-run every i == j and i == k check on paper.
2. Same formula. Nothing changes except rows — proving the pattern scales.
3. Mirror still holds. Row 2 prints 2 2 — left digit at j = 2, right digit at k = 2.
Edge Cases & Pitfalls
Check these before calling the solution done.
Triple digit
Start the right loop at k = 2
Starting at 1 reprints the center column and can show three digits on some rows.
Newlines
Do not put "\n" inside an inner loop
That turns the pattern into a vertical list of single characters.
Alignment
View output in a monospace font
Proportional fonts hide the inverse-V even when the spaces are correct.
Bad cin
Validate input
Check cin >> rows and require rows >= 1 before the loops.
Analysis
Time and Space Complexity
Program
Time
Extra space
Pyramid (Examples 1–3)
O(n²)
O(1)
Each of n rows scans about 2n - 1 character slots — quadratic in n. Extra memory is only a few loop variables.
Remember
Key Takeaways
Rule: left j = rows..1, right k = 2..rows, digit when equal to i.
Shape: two digits per row form an inverse V of numbers.
Break the row: call cout << "\n" only after both scans finish.
Complexity:O(n²) time, O(1) extra space.
One line: print the row digit on both diagonals; fill every other column with a space.
Frequently Asked Questions
The left loop places the digit on the left diagonal; the right loop mirrors it on the right diagonal.
Starting at 2 avoids duplicating the center column — each row prints the digit at most twice.
For rows=5: an inverse-V of digits — row 1 has a centered 1; row 5 prints 5 at both ends with spaces between.
Program 56 prints a full palindromic row (1..i..1). Program 57 prints only the row digit twice on mirror diagonals.
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 each row scans about 2n character positions.
One row prints a single 1 — the right loop (k = 2..1) does not run.
🤔
Did you know?
Each row prints the row number twice — once on the left diagonal and once on the right — with spaces everywhere else. Total positions per row = 2×rows-1.