Monday, August 24, 2026

Printing Hollow Diamond in C | Advanced Shape Patterns

The hollow diamond pattern is a classic logic-building exercise in C programming. Unlike solid shape patterns, printing a hollow structure requires combining space management, structural symmetry, and conditional boundary checks (if-else statements) within nested loops.

Introduction

Constructing a hollow diamond involves dividing the pattern into two symmetrical phases—an upper growing pyramid and a lower shrinking pyramid. The key challenge lies in replacing inner asterisks with spaces while ensuring the outer perimeter remains intact. This tutorial breaks down the mathematical logic, provides a full C implementation, and analyzes its performance complexity.

  • Prerequisites: Proficiency in nested for loops, conditional logic (if-else), standard input/output (printf, scanf), and arithmetic operations.

  • Expected Output:

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

Deconstructing the Pattern Logic

For an upper-half height of $n$, the total height of the shape is 2n - 1 rows. We split the rendering logic into two phases:

Phase 1: Upper Hollow Pyramid (i = 1 to n)

  • Leading Spaces: Decreases each row (n - i).

  • Boundary Condition: Print * only at the first (j = 1) and last (j = 2i - 1) column positions. Fill all intermediate positions (1 < j < 2i - 1) with empty spaces.

Phase 2: Lower Inverted Hollow Pyramid (i = 1 to n - 1)

  • Leading Spaces: Increases each row (i).

  • Boundary Condition: Print * only at the first (j = 1) and last (j = 2(n - i) - 1) column positions. Fill intermediate positions with spaces.

Row IndexPhaseLeading SpacesStar Indices (j)Inner Spaces
1Upper410
2Upper31, 31
3Upper21, 53
4Upper11, 75
5 (Center)Upper01, 97
1 (Row 6)Lower11, 75
2 (Row 7)Lower21, 53
3 (Row 8)Lower31, 31
4 (Row 9)Lower410

Code Implementation

#include <stdio.h>

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

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

    // Phase 1: Upper Hollow Pyramid (Rows 1 to n)
    for (i = 1; i <= n; i++) {
        // Print leading spaces
        for (space = 1; space <= n - i; space++) {
            printf(" ");
        }
        // Print boundary asterisks and inner spaces
        for (j = 1; j <= (2 * i - 1); j++) {
            if (j == 1 || j == (2 * i - 1)) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    // Phase 2: Lower Inverted Hollow Pyramid (Rows 1 to n - 1)
    for (i = 1; i <= n - 1; i++) {
        // Print leading spaces
        for (space = 1; space <= i; space++) {
            printf(" ");
        }
        // Print boundary asterisks and inner spaces
        for (j = 1; j <= (2 * (n - i) - 1); j++) {
            if (j == 1 || j == (2 * (n - i) - 1)) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}

Code Breakdown

  • Boundary Checks: The condition j == 1 || j == (2 * i - 1) restricts character printing solely to the edges of the shape, creating the hollow interior.

  • Phase Split: Executing Phase 2 up to n - 1 prevents duplicating the central apex line.

  • Row Transition: printf("\n"); ensures proper line breaks after completing each horizontal scan line.

Compiling and Execution

Compile and execute using GCC:

Console Output:

Enter the number of rows for upper half: 5
    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *

Common Mistakes & Troubleshooting

  • Filling the Interior: Omitting conditional statements within the character loop results in a standard solid diamond rather than a hollow outline.

  • Off-by-One Boundary Errors: Checking j == i instead of j == (2 * i - 1) distorts the right boundary line.

Complexity Analysis

  • Time Complexity: O(n^2) due to nested space allocation and character loops across 2n - 1 total iterations.

  • Space Complexity: O(1) auxiliary memory since only scalar integer counters are used.

Conclusion

Combining boundary conditions with multi-phase nested loops provides a foundational technique for rendering custom graphical boundaries in terminal applications.


For all Pattern Programs list click here

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