A mirror diagonal number pattern places digit i on the main diagonal and again on a mirrored diagonal, with spaces everywhere else — forming a V-shape. With rows = 5 both arms show 1..5 meeting at a single tip.
Remember
Rule: print digit when i==j (left) or i==k (right); else space
1 1
2 2
3 3
4 4
5 ← rows = 5
Unlike Program 52 (palindromic digit runs), this shape uses conditional placement — i == j and i == k — with spaces between.
Approach
How to Solve It
Outer loop picks row i. Left half scans j = 1..rows; right half scans k = rows-1..1.
Method
Idea
Best for
Two half-loops
Left i == j, right i == k, spaces elsewhere
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 1 to rows:
print digit if i == j else space
for k from (rows - 1) down to 1:
print digit if i == k else space
print newline
Cheat sheet
Goal
Pattern
Outer rows
for (i = 1; i <= rows; i++)
Left diagonal
for (j = 1; j <= rows; j++) (i == j ? cout << j : cout << " ");
Right diagonal
for (k = rows - 1; k >= 1; k--) (i == k ? cout << k : cout << " ");
Skip center
Right loop starts at rows - 1
End the row
cout << "\n";
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << j / cout << " "
Stays on the same line
Each cell (digit or space)
cout << "\n"
Ends the current line
After both inner loops
Print cells without a newline, then end the row once. Spaces are still “writes” — they keep the diagonals aligned.
Try it
Live Preview
Change the row count and the V-shaped diagonals update instantly.
Use rows from 2 to 9. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 9 cols
1 1
2 2
3 3
4 4
5
Trace
Worked Walkthrough
Trace row i = 3 with rows = 5 — left half then right half.
Half
Scan
Where digit prints
Partial line
Left
j = 1..5
j == 3
3
Right
k = 4..1
k == 3
3 3
Starting the right loop at rows - 1 skips the center column so the tip (row 5) shows a single 5.
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 — print a digit when i == j or i == k, otherwise a space.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 5;
int i, j, k;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= rows; j++)
(i == j ? cout << j : cout << " ");
for (k = rows - 1; k >= 1; k--)
(i == k ? cout << k : cout << " ");
cout << "\n";
}
return 0;
}
Output
1 1
2 2
3 3
4 4
5
How It Works
1. Left half places the main diagonal. For each j, print j when i == j, otherwise a space.
2. Right half mirrors it.k counts from rows - 1 down to 1; print when i == k.
3. Tip is a single digit. On the last row both arms meet; skipping the center in the right loop avoids a double tip.
Example 2 — User Input Rows
Read the row count at runtime — both diagonals scale automatically.
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 = 1; j <= rows; j++)
(i == j ? cout << j : cout << " ");
for (k = rows - 1; k >= 1; k--)
(i == k ? cout << k : cout << " ");
cout << "\n";
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
1 1
2 2
3 3
4
How It Works
1. Same two-loop core. Only the source of rows changes from a literal to cin.
2. Width follows rows. Entering 4 yields a narrower V ending at a single 4.
3. Validate in real apps. Prefer checking cin failure and requiring rows >= 1 (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 every i == j and i == k check.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 3;
int i, j, k;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= rows; j++)
(i == j ? cout << j : cout << " ");
for (k = rows - 1; k >= 1; k--)
(i == k ? cout << k : cout << " ");
cout << "\n";
}
return 0;
}
Output
1 1
2 2
3
How It Works
1. Only three rows. Easy to dry-run every column check on paper.
2. Same formula. Nothing changes except rows — proving the pattern scales.
3. Tip is still unique. Row 3 prints a single centered 3.
Edge Cases & Pitfalls
Check these before calling the solution done.
Right start
Start the right loop at rows - 1
Starting at rows duplicates the center tip on the last row.
Condition
Print when equal, not when unequal
Use i == j / i == k. Using i != j fills the row with digits and breaks the V.
Spaces
Always print a space on misses
Without spaces, the diagonals collapse and lose alignment.
Bad cin
Validate input
Check cin >> rows and require rows >= 1 before the loops.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–3)
O(n²)
O(1)
Each of n rows prints about 2n - 1 characters, so work is quadratic in n. Only a few integers of extra memory.
Remember
Key Takeaways
Rule: print the digit when i == j (left) or i == k (right); else a space.
Skip the center: right loop starts at rows - 1 so the tip is unique.
Break the row: call cout << "\n" only after both half-loops.
Complexity:O(n²) time; O(1) extra space.
One line: for each row, place digit i on both diagonals with spaces between, then end the line.
Frequently Asked Questions
It prints the row number on the main diagonal (left) and on a mirrored diagonal (right), creating a symmetric V-shape.
The first loop prints the left half across rows columns. The second prints the right mirrored half across rows-1 columns in reverse.
Skipping the center column prevents printing the middle digit twice where the diagonals would meet.
On the final row both diagonals meet at the tip. The right loop starts at rows-1, so only the left half prints the center digit.
Program 52 builds palindromic digit rows with m++ and m--. Program 53 uses spaces and i == j / i == k to place digits on mirrored diagonals.
Printing a digit or space stays on the same line. Printing a newline ends the current row. Cells use cout without a newline; the row break uses cout << "\n" after both inner loops.
O(n²) for n rows because each row prints about 2n-1 characters using nested loops.
After cin >> rows, check failure and require rows >= 1 before the loops.
🤔
Did you know?
Each row prints the row digit on the main diagonal (i == j) and on a mirrored diagonal (i == k). About 2n - 1 characters per row — total work O(n²).