C++ Diagonal Mirror Number Pattern

Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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.

How to Solve It

Outer loop picks row i. Left half scans j = 1..rows; right half scans k = rows-1..1.

MethodIdeaBest for
Two half-loopsLeft i == j, right i == k, spaces elsewhereLearning, interviews, demos
Compact traceSame logic with rows = 3Quick 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

GoalPattern
Outer rowsfor (i = 1; i <= rows; i++)
Left diagonalfor (j = 1; j <= rows; j++) (i == j ? cout << j : cout << " ");
Right diagonalfor (k = rows - 1; k >= 1; k--) (i == k ? cout << k : cout << " ");
Skip centerRight loop starts at rows - 1
End the rowcout << "\n";

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << j / cout << " "Stays on the same lineEach cell (digit or space)
cout << "\n"Ends the current lineAfter both inner loops

Print cells without a newline, then end the row once. Spaces are still “writes” — they keep the diagonals aligned.

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 result 5 rows · 9 cols
1       1
 2     2 
  3   3  
   4 4   
    5    

Worked Walkthrough

Trace row i = 3 with rows = 5 — left half then right half.

HalfScanWhere digit printsPartial line
Leftj = 1..5j == 33
Rightk = 4..1k == 33 3

Starting the right loop at rows - 1 skips the center column so the tip (row 5) shows a single 5.

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;
}

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;
}

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;
}

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.

Time and Space Complexity

ProgramTimeExtra 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.

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²).

Next: Mirror Diagonal Diamond

Continue with the next pattern in the C++ number-pattern series.

Program 54 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful