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

Hollow Half Diamond Star Pattern

Read summary with AI tools

The hollow half diamond star pattern keeps only the outline of the half diamond: a straight left edge and a slanted right edge that points out to the right. Some sites call it the hollow right Pascal’s triangle.

Quick answer: Print a hollow right triangle for rows 1 to n, then the same rows again from n − 1 back down to 1. On row i a position gets a star only when j == 1 or j == i; the positions in between are spaces.

This program is part of the Star Patterns collection.

Table of contents8 sections
  1. Output
  2. How the hollow half diamond star pattern 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 Half Diamond Star Pattern output

Output (n = 5):

*
**
* *
*  *
*   *
*  *
* *
**
*
Hollow Half Diamond Star Pattern layout diagram, row by row (n = 5)
How each row of the hollow half diamond star pattern is built (n = 5). Blue cells are printed characters; grey dots are spaces.

How the hollow half diamond star pattern works

The solid half diamond prints i stars per row, growing and then shrinking. The hollow version prints the same i positions but fills only the first and the last, which are the two edges. Row 1 and row 2 have no inside yet, so they look solid. Running the second half from n − 1 keeps the widest row from being printed twice.

  1. Run the outer loop for i from 1 to n, one row per pass (upper half).
  2. Go through i positions in the row. Print stars where j = 1 or j = i; print spaces everywhere else.
  3. Move to the next line.
  4. Run the outer loop for i from n − 1 down to 1, one row per pass (lower half).
  5. Go through i positions in the row. Print stars where j = 1 or j = i; print spaces everywhere else.
  6. Move to the next line.

Key takeaways

  • A hollow shape is the solid shape with an edge test in the inner loop.
  • The two edges are the first position (j == 1) and the last one (j == i).
  • The lower half starts at n − 1 so the middle row appears once.
  • The gap inside grows by one space per row, up to n − 2 spaces in the middle.

Hollow Half Diamond Star Pattern in C

#include <stdio.h>

int main(void) {
    int n = 5;

    // upper half
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            if (j == 1 || j == i)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }

    // lower half
    for (int i = n - 1; i >= 1; i--) {
        for (int j = 1; j <= i; j++) {
            if (j == 1 || j == i)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

Hollow Half Diamond Star Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 5;

    // upper half
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            if (j == 1 || j == i)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }

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

Hollow Half Diamond Star Pattern in Java

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

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

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

Hollow Half Diamond Star Pattern in Python

n = 5

# upper half
for i in range(1, n + 1):
    for j in range(1, i + 1):
        if j == 1 or j == i:
            print("*", end="")
        else:
            print(" ", end="")
    print()

# lower half
for i in range(n - 1, 0, -1):
    for j in range(1, i + 1):
        if j == 1 or j == i:
            print("*", 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 do the first two rows look solid?

Row 1 has a single position and row 2 has two, so every position is an edge. The gap only appears once a row has three or more positions.

How do I point it to the left instead?

Right-align the rows: print stars where j == n − i + 1 or j == n on an n-wide row. That is the hollow mirrored half diamond.

How do I let the user choose the size?

Read n from the keyboard instead of fixing it in the code: scanf("%d", &n); in C, cin >> n; in C++, new Scanner(System.in).nextInt() in Java and int(input()) in Python.

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.

Latest articles

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.