Hollow Number Pyramid (Diagonal Print) in C++

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Conditional Printing

What You’ll Learn

How to print a hollow number pyramid in C++ where each row prints the row number at two diagonal positions (left and right), and spaces everywhere else.

This is a useful exercise for learning nested loops + if/else conditions to decide what to print at each position.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
     1
    2 2
   3   3
  4     4
 5       5
1

Complete C++ Program

We scan across columns and print the number only when the current column matches the diagonal position for that row. Otherwise we print a space.

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

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

    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

Choose the row count

int rows = 5; controls how many lines are printed.

Setup
2

Outer loop prints rows

for (i = 1; i <= rows; i++) runs once per row and decides which number should appear on that row.

Row control
3

Left diagonal (count down columns)

for (j = rows; j >= 1; j--) prints the left side. When i == j, we print j; otherwise we print a space.

Left edge
4

Right diagonal (count up columns)

for (k = 2; k <= rows; k++) prints the right side. When i == k, we print k; otherwise we print a space.

Right edge
=

Hollow number pyramid

Only two positions per row print a number. Checking each position makes the runtime roughly O(n²) for n rows.

2

Variation — User Input Version

Let the user choose how many rows to print using cin.

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Replace spaces with . temporarily to visualize alignment while debugging
  • Print * instead of numbers to create a hollow star pyramid
  • Print the full pyramid (fill inside) by removing the conditions
  • Build a diamond by printing this pyramid and then reversing it
  • Add input validation to reject non-positive row counts

Avoid

  • Forgetting the newline after each row
  • Hardcoding 5 in multiple places instead of using rows
  • Using tabs inconsistently (they render differently in consoles)
  • Printing endl in tight loops (slower than "\n")

Key Takeaways

1

This is a hollow pattern: only diagonal positions print numbers.

2

Use conditional checks inside loops to decide between a number and a space.

3

Split printing into two halves (left and right) to keep logic simple.

4

The same idea works for other shapes like hollow stars.

❓ Frequently Asked Questions

On the top row, both diagonals meet at a single point, so you see only 1.
Print fewer spaces when the condition fails, or use a different spacing strategy. Many consoles render multiple spaces the same, so experiment with one space vs two spaces.
Yes. Remove the if conditions and print the row number (or a sequence) for every position you want filled.
O(n²) for n rows, since we check each potential print position in the nested loops.

Explore More C++ Number Patterns!

Learn more variations of hollow and filled pyramids, triangles, and diamonds.

All Number Patterns →
Did you know?

Hollow patterns are great practice for condition-based printing. Once you master this, you can build framed rectangles, hollow diamonds, and more using the same idea.

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