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

Hollow Number Triangle

The hollow number triangle keeps only the edges of the number right triangle: the first column, the slanted side and the bottom row. Every number stays in the column it would have in the full triangle.

Quick answer: Use the normal triangle loops and test each position. Print j when it is on an edge, that is when j == 1, j == i or i == n, and print a space otherwise. Printing j rather than a fixed digit keeps every number in its own column.

This program is part of the Number Patterns collection.

Table of contents8 sections
  1. Output
  2. How the hollow number triangle 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 Triangle output

Output (n = 5):

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

Same shape, other characters: the Hollow Alphabet Triangle and Hollow Right Triangle 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 triangle works

A hollow shape is the solid shape with an extra test inside the inner loop. The three edges each have a simple condition: the left edge is the first position, the slanted edge is the last position of the row, and the bottom edge is the whole last row. Positions that pass none of the tests are inside the triangle and become spaces, which leaves a gap that grows by one each row.

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

Key takeaways

  • A hollow pattern is the solid pattern plus an edge test.
  • The three edges are j == 1, j == i and i == n.
  • Printing j keeps each number in its own column, so the gaps line up.
  • The inside gap grows by one space per row until the full bottom row closes the shape.

Hollow Number Triangle in C

#include <stdio.h>

int main(void) {
    int n = 5;

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

Hollow Number Triangle in C++

#include <iostream>
using namespace std;

int main() {
    int n = 5;

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

Hollow Number Triangle in Java

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

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

Hollow Number Triangle in Python

n = 5

for i in range(1, n + 1):
    for j in range(1, i + 1):
        if j == 1 or j == i or i == n:
            print(j, 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 print j and not i on the edges?

With j, the column number, the bottom row reads 12345 and the slanted edge shows each row’s length. Printing i instead fills each edge with the row number: 1, 22, 3 3, 4 4, 55555.

How do I make a hollow number square?

Use n positions on every row and add j == n and i == 1 to the edge test. That gives the hollow number square.

Why is row 2 not hollow?

It only has two positions and both are edges, so there is no inside yet. The gap appears from row 3 on.

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 19 of 24 in Number 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.