New: 214 pattern programs with tested code in C, C++, Java and Python Explore patterns

Hollow Number Pyramid

The hollow number pyramid draws only the outline of a pyramid, with each edge written in its row number: 1 at the top, then 2 2, 3 3, 4 4, and a full base of 5s.

Quick answer: Treat row i as n + i βˆ’ 1 positions. Print the row number i on the two slanted edges, positions n βˆ’ i + 1 and n + i βˆ’ 1, and on every position of the last row; print a space everywhere else.

This program is part of the Pyramid Patterns collection.

Table of contents8 sections
  1. Output
  2. How the hollow number pyramid works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Hollow Number Pyramid output

Output (n = 5):

    1
   2 2
  3   3
 4     4
555555555
Hollow Number Pyramid layout diagram, row by row (n = 5)
How each row of the hollow number pyramid is built (n = 5). Blue cells are printed characters; grey dots are spaces.

Same shape, other characters: the Hollow Alphabet Pyramid and Hollow Pyramid Star Pattern programs draw this same outline with different characters. Putting them side by side shows which part of the loop decides the shape and which part decides what is printed.

How the hollow number pyramid works

This is the hollow pyramid with digits instead of stars. Row i of a pyramid covers the columns from n βˆ’ i + 1 to n + i βˆ’ 1; anything before that is indent. Inside the row only the two end positions are printed, except on the base, which is printed in full to close the shape. Writing the row number on each edge shows clearly which row each part of the outline belongs to.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Go through n + i βˆ’ 1 positions in the row. Print numbers (value i) where j > n βˆ’ i and (i = n or j = n βˆ’ i + 1 or j = n + i βˆ’ 1); print spaces everywhere else.
  3. Move to the next line.

Key takeaways

  • Row i runs from column n βˆ’ i + 1 to n + i βˆ’ 1.
  • Only the two ends of each row and the full base are printed.
  • The condition j > n βˆ’ i skips the indent in front of the row.
  • Replace i with a star and you have the hollow pyramid star pattern.

Hollow Number Pyramid in C

#include <stdio.h>

int main(void) {
    int n = 5;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n + i - 1; j++) {
            if (j > n - i && (i == n || j == n - i + 1 || j == n + i - 1))
                printf("%d", i);
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

Hollow Number Pyramid in C++

#include <iostream>
using namespace std;

int main() {
    int n = 5;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n + i - 1; j++) {
            if (j > n - i && (i == n || j == n - i + 1 || j == n + i - 1))
                cout << i;
            else
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}

Hollow Number Pyramid in Java

public class HollowNumberPyramid {
    public static void main(String[] args) {
        int n = 5;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n + i - 1; j++) {
                if (j > n - i && (i == n || j == n - i + 1 || j == n + i - 1))
                    System.out.print(i);
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

Hollow Number Pyramid in Python

n = 5

for i in range(1, n + 1):
    for j in range(1, n + i):
        if j > n - i and (i == n or j == n - i + 1 or j == n + i - 1):
            print(i, end="")
        else:
            print(" ", end="")
    print()

Time and space complexity

The program prints one character for each position in the pattern, so for n rows it runs in O(nΒ²) time. It only needs a few loop variables, so the extra memory is O(1).

Frequently asked questions

Why is the base all 5s?

The last row is printed in full to close the shape, and every position on it prints the row number, which is 5. Print j on the base instead if you want 123456789.

How do I make the numbers on the base count?

Use a separate value for the last row, for example j βˆ’ n + i, or print j there. The edges keep showing the row number.

Why test j > n βˆ’ i at all?

Without it, the condition i == n would be checked for the leading spaces too. Keeping the indent out of the test makes the conditions easier to read and avoids printing digits in front of the row.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 12 of 19 in Pyramid Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.