Hollow Number Diamond in C++

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Two-Part Pattern

What You’ll Learn

How to print a hollow number diamond in C++. The top half is a hollow pyramid (1 to rows) and the bottom half mirrors it back down to 1.

This pattern is ideal for learning how to combine nested loops with conditions to print either a digit or a space.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
     1
    2 2
   3   3
  4     4
 5       5
  4     4
   3   3
    2 2
     1
1

Complete C++ Program

We print the top half (1..rows) and then the bottom half (rows-1..1). Each half uses the same diagonal-print logic.

C++
#include <iostream>
using namespace std;

int main() {
    int i, j, k;
    int rows = 5;

    // First Part (top half)
    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";
    }

    // Second Part (bottom half)
    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

Split into two halves

The diamond is created by printing a top half (i = 1..rows) and a bottom half (i = rows-1..1).

Structure
2

Left diagonal printing

Loop j from rows down to 1. Print j only when i == j; otherwise print spaces.

Left edge
3

Right diagonal printing

Loop k from 2 to rows. Print k only when i == k; otherwise print spaces.

Right edge
4

Mirror the top half

The bottom half uses the same logic but runs i downward. That creates a symmetric diamond shape.

Mirror
=

Hollow number diamond

Because we scan across positions with nested loops, the runtime grows roughly like O(n²) for n rows.

2

Variation — User Input Version

Use cin to choose the size of the diamond at runtime.

C++
#include <iostream>
using namespace std;

int main() {
    int i, j, k;
    int rows;

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

💡 Tips for Enhancement

Try These

  • Replace spaces with . temporarily to debug alignment
  • Print * to create a hollow star diamond
  • Print row numbers with a trailing space for consistent width (e.g., cout << i << ' ')
  • Build a filled diamond by printing numbers across the full width
  • Wrap top/bottom printing into a function for reuse

Avoid

  • Hardcoding row counts throughout the loops
  • Forgetting the rows - 1 start for the bottom half (it would duplicate the middle row)
  • Mixing tabs and spaces (console output can shift)
  • Using endl repeatedly in loops (slower)

Key Takeaways

1

Make a diamond by printing a pyramid up and then down.

2

Use conditions to print numbers only on the diagonal edges.

3

Start the bottom half at rows - 1 to avoid duplicating the middle row.

4

This technique generalizes to many shapes, including diamonds and hollow pyramids.

❓ Frequently Asked Questions

To avoid printing the widest middle row twice. The top half already prints the rows line, so the bottom half begins at rows - 1.
Yes. You can print two spaces instead of one, or print a trailing space after each number to keep the output aligned for multi-digit rows.
The idea is the same: print characters only at boundary positions (diagonals). Here we print the row number instead of *.
O(n²) for n rows due to nested loops scanning print positions.

Explore More C++ Number Patterns!

Keep practicing with hollow, filled, and symmetric patterns to master loops.

All Number Patterns →
Did you know?

Many diamond patterns are made by combining two triangles or pyramids. Once you recognize the halves, you can build new shapes quickly by reusing the same loop logic.

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