C++ Hollow Number Pyramid Pattern

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

What Is This Pattern?

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.

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.

MethodIdeaBest for
Two scansLeft diagonal + right diagonalLearning, interviews, demos
Compact traceSame logic with rows = 3Quick 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

GoalPattern
Outer row loopfor (i = 1; i <= rows; i++)
Left diagonalfor (j = rows; j >= 1; j--) then if (i == j)
Right diagonalfor (k = 2; k <= rows; k++) then if (i == k)
Digit or spacecout << j; / cout << " ";
End the rowcout << "\n";
Width per row2 * rows - 1 character slots

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

Worked Walkthrough

Trace row i = 3 when rows = 5 — left scan, then right scan.

StepLoopPrints
1Left: j = 5..13 (digit at j = 3)
2Right: k = 2..53 (digit at k = 3)
ResultFull row 33 3

Starting the right loop at k = 2 skips the center so the digit is not printed three times.

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

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

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

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.

Time and Space Complexity

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

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.

Next: Diagonal Mirror Number Diamond

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

Program 58 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