C++ Hollow Number Diamond Pattern

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

What Is This Pattern?

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.

Remember
Rule: top i = 1..rows; bottom i = rows-1..1 (same diagonal scans)

    1
   2 2
  3   3
 4     4
5       5
 4     4
  3   3
   2 2
    1     ← rows = 5 (9 lines)

Unlike Program 57 (top half only), this page adds for (i = rows - 1; i >= 1; i--) so the shape closes into a diamond.

How to Solve It

Reuse the same left/right diagonal logic twice: once counting up to rows, then counting down from rows - 1.

MethodIdeaBest for
Two outer loopsTop half + mirrored bottomLearning, interviews, demos
Compact traceSame logic with rows = 3Quick 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

GoalPattern
Top halffor (i = 1; i <= rows; i++)
Bottom halffor (i = rows - 1; i >= 1; i--)
Left diagonalfor (j = rows; j >= 1; j--) then if (i == j)
Right diagonalfor (k = 2; k <= rows; k++) then if (i == k)
End the rowcout << "\n";
Total lines2 * rows - 1

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << j / cout << " "Stays on the same lineEach column slot
cout << "\n"Ends the lineAfter both inner loops

Print digits and spaces without a newline, then end the row once.

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

Worked Walkthrough

Trace how rows = 5 builds nine lines — top half, then bottom half without repeating the peak.

PhaseOuter iLines printed
Top1..51 … 5 5
Bottom4..14 4 … 1
SkipPeak not repeatedbottom starts at rows-1

Inner loops are identical in both halves — only the outer direction changes.

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

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

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

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.

Time and Space Complexity

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

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.

Next: Hollow Square Border Numbers

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

Program 59 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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