Thursday, August 27, 2026

Printing Hollow Rhombus in C | Advanced Shape Patterns

The hollow rhombus pattern extends the solid rhombus concept by introducing boundary checks within nested loops, printing asterisks only along the outer perimeter of the parallelogram.

Introduction

Mastering this shape sharpens your ability to pair horizontal space offsets with conditional checks (if-else statements) to isolate interior spaces from exterior borders. This tutorial breaks down the visual logic, step-by-step algorithm, C source code, and performance complexity required to output a flexible hollow rhombus based on user input.

  • Prerequisites: Knowledge of nested for loops, conditional logic (if-else), standard I/O (printf, scanf), and basic arithmetic.

  • Expected Output:

    *****
   *    *
  *    *
 *    *
*****

Deconstructing the Pattern Logic

A hollow rhombus of size n consists of n rows and n columns. Each row is shifted by n - i leading spaces to create the slanted parallelogram alignment.

Inside the character loop, asterisks are printed only along the four borders:

  • Top and Bottom Edges: On the first row (i = 1) and last row (i = n), every column position from j = 1 to n prints an asterisk (*).

  • Side Edges: On middle rows (1 < i < n), asterisks are printed only at the first column (j = 1) and last column (j = n), while intermediate positions (1 < j < n) print spaces.

Row (i)Leading Spaces (n−i)Border Condition (j=1 or j=n or i=1 or i=n)Character Output
1 (Top)4All j positions valid*****
23j = 1 and j = 5* *
32j = 1 and j = 5* *
41j = 1 and j = 5* *
5 (Bottom)0All j positions valid*****

Code Implementation

#include <stdio.h>

int main() {
    int n, i, j, space;

    printf("Enter the number of rows: ");
    if (scanf("%d", &n) != 1 || n <= 0) {
        printf("Invalid input. Please enter a positive integer.\n");
        return 1;
    }

    for (i = 1; i <= n; i++) {
        // Print leading spaces for slant alignment
        for (space = 1; space <= n - i; space++) {
            printf(" ");
        }
        // Print boundary asterisks and inner spaces
        for (j = 1; j <= n; j++) {
            if (i == 1 || i == n || j == 1 || j == n) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}

Code Breakdown

  • Space Offsets: The loop space <= n - i calculates leading space counts to tilt the shape into a rhombus.

  • Boundary Logic: The condition if (i == 1 || i == n || j == 1 || j == n) ensures solid horizontal boundaries for top/bottom rows and solid vertical boundaries for side edges.

  • Interior Hollow Space: Any character index not lying on a boundary defaults to printing a blank space.

Compiling and Execution

Compile and run the program using standard GCC tools:

Console Output:
Plaintext
Enter the number of rows: 5
    *****
   *    *
  *    *
 *    *
*****

Common Mistakes & Troubleshooting

  • Missing Top/Bottom Checks: Omitting i == 1 || i == n leaves only side walls standing, causing top and bottom borders to stay open.

  • Distorted Slant: Using i - 1 instead of n - i for leading spaces slants the rhombus in the opposite direction.

  • Missing Line Breaks: Forgetting printf("\n"); outputs all characters onto a single horizontal line.

Complexity Analysis

  • Time Complexity: O(n^2) because the outer loop executes n times containing two inner loops iterating up to n times.

  • Space Complexity: O(1) auxiliary memory space, using only scalar integer variables.

Conclusion

Integrating multi-condition boundary checks into fixed-width nested loops allows you to render custom hollow geometric boundaries in terminal applications.


For all Pattern Programs list click here

…till the next post, bye-bye & take care

No comments:

Post a Comment