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

Hollow Parallelogram Star Pattern

Read summary with AI tools

The hollow parallelogram star pattern draws only the border of a slanted rectangle: a full top and bottom row of stars and two parallel slanted sides. It is the outline of the parallelogram star pattern.

Quick answer: Row i starts with n − i spaces and then covers 2n columns, from n − i + 1 to 3n − i. Print a star on the first and last row and at the first and last column of each row’s block; everything else is a space.

This program is part of the Star Patterns collection.

Table of contents8 sections
  1. Output
  2. How the hollow parallelogram 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 Parallelogram Star Pattern output

Output (4 rows, 8 columns):

   ********
  *      *
 *      *
********
Hollow Parallelogram Star Pattern layout diagram, row by row (4 rows, 8 columns)
How each row of the hollow parallelogram star pattern is built (4 rows, 8 columns). Blue cells are printed characters; grey dots are spaces.

How the hollow parallelogram star pattern works

The solid parallelogram is a block of 2n stars that shifts one place left each row. To hollow it out, keep the same block but test where you are in it. Top and bottom rows are printed in full; on the rows in between only the two ends of the block are stars. The first part of the test, j > n − i, keeps the indent out of the picture.

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

Key takeaways

  • Each row covers columns n − i + 1 to 3n − i: 2n wide, shifted left each row.
  • Full top and bottom rows close the shape.
  • The two slanted sides are the first and last column of the block.
  • The condition j > n − i skips the leading spaces.

Hollow Parallelogram Star Pattern in C

#include <stdio.h>

int main(void) {
    int n = 4;

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

Hollow Parallelogram Star Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 4;

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

Hollow Parallelogram Star Pattern in Java

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

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

Hollow Parallelogram Star Pattern in Python

n = 4

for i in range(1, n + 1):
    for j in range(1, 3 * n - i + 1):
        if j > n - i and (i == 1 or i == n or j == n - i + 1 or j == 3 * n - 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

How do I make it lean the other way?

Indent each row by i − 1 spaces instead, as in the mirrored hollow parallelogram. The block then moves right as you go down.

How do I change the width?

Replace 2n with any width w: the block runs from n − i + 1 to n − i + w, and the right edge test becomes j == n − i + w.

Why is the bottom row not indented?

On the last row n − i = 0, so the block starts at the left margin. Each row above it starts one column further right.

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.