Friday, August 28, 2026

Printing Hourglass in C | Advanced Shape Patterns

The hourglass star pattern is an iconic multi-phase geometry problem in C programming that tests your ability to mirror logic by connecting an inverted full pyramid seamlessly to a standard full pyramid.

Introduction

Mastering the hourglass pattern refines your mastery over multi-loop coordination, dynamic bounds calculation, and vertical symmetry. This tutorial breaks down the architectural logic, line-by-line algorithm, complete C source code, and performance complexity required to output a flexible hourglass pattern based on user input.

  • Prerequisites: Proficiency in nested for loops, standard input/output (printf, scanf), and integer arithmetic.

  • Expected Output:

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

Deconstructing the Pattern Logic

For a half-height of n, the total height of the hourglass is 2n - 1 rows. We split the structure into two sequential rendering phases:

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

  • Leading Spaces: Increases each row from 0 to n - 1 (i - 1 spaces).

  • Asterisks: Decreases in odd increments given by 2(n - i) + 1.

Phase 2: Lower Full Pyramid (i = 2 to n)

  • Leading Spaces: Decreases each row from n - 2 down to 0 (n - i spaces).

  • Asterisks: Increases in odd increments given by 2i - 1.

Row IndexPhaseLeading SpacesAsterisks
1Upper09
2Upper17
3Upper25
4Upper33
5 (Center)Upper41
2 (Row 6)Lower33
3 (Row 7)Lower25
4 (Row 8)Lower17
5 (Row 9)Lower09

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 Inverted Pyramid (Rows 1 to n)
    for (i = 1; i <= n; i++) {
        // Print leading spaces
        for (space = 1; space < i; space++) {
            printf(" ");
        }
        // Print asterisks
        for (j = 1; j <= (2 * (n - i) + 1); j++) {
            printf("*");
        }
        printf("\n");
    }

    // Phase 2: Lower Full Pyramid (Rows 2 to n)
    for (i = 2; i <= n; i++) {
        // Print leading spaces
        for (space = 1; space <= n - i; space++) {
            printf(" ");
        }
        // Print asterisks
        for (j = 1; j <= (2 * i - 1); j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Code Breakdown

  • Upper Loop Phase: Iterates through rows 1 to n, reducing asterisks from 2n - 1 down to 1 to form the contracting top half.

  • Lower Loop Phase: Starts at i = 2 to prevent duplicating the single central asterisk vertex, expanding back out to 2n - 1 asterisks.

  • Line Break Sequencing: printf("\n"); executes after each inner character loop completes, ensuring precise row alignment.

Compiling and Execution

Compile and run the program using GCC:

Console Output:

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

Common Mistakes & Troubleshooting

  • Duplicate Single Star Vertex: Starting Phase 2 at i = 1 prints two consecutive rows with a single asterisk (*), distorting the central focal point.

  • Off-by-One Space Shift: Using space <= i instead of space < i in Phase 1 adds an unnecessary extra leading space on the first row.

  • Incorrect Odd Count Formulas: Mixing up upper (2(n - i) + 1) and lower (2i - 1) star formulas breaks the symmetry.

Complexity Analysis

  • Time Complexity: O(n^2) because two sequential nested loop blocks execute outer iterations up to $n$ times with inner loops proportional to n.

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

Conclusion

Symmetrically coupling inverted and upright nested loops enables you to construct complex hourglass geometries cleanly.



For all Pattern Programs list click here

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

No comments:

Post a Comment