Open In App

C Program to Print Hollow Hourglass Pattern

Last Updated : 13 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Hollow Hourglass Pattern is a symmetrical pattern that resembles an hourglass but with hollow spaces inside and only edges having characters. In this article, we will learn how to print the Hollow Hourglass Pattern using a C program.

Hollow-Hourglass-Pattern

Program to Print Hollow Hourglass Pattern in C

Star Pattern
#include <stdio.h>

int main() {
    int n = 5;

    // Outer loop to iterate through each row
    for (int i = 0; i < 2 * n - 1; i++) {

        // Determine the row level relative to the center
        int l = (i < n) ? i : 2 * n - 2 - i;

        // Print leading spaces
        for (int j = 0; j < l; j++) {
            printf("  ");
        }

        // Print stars and hollow spaces in the current row
        for (int k = 0; k < 2 * (n - l) - 1; k++) {
            if (k == 0 || k == 2 * (n - l) - 2 || i == 0
                || i == 2 * n - 2)
                printf("* ");
			else
                printf("  ");
        }
        printf("\n");
    }

    return 0;
}
Number Pattern
#include <stdio.h>

int main() {
    int n = 5;

    // Outer loop to iterate through each row
    for (int i = 0; i < 2 * n - 1; i++) {

        // Determine the row level relative to the center
        int l = (i < n) ? i : 2 * n - 2 - i;

        // Print leading spaces
        for (int j = 0; j < l; j++) {
            printf("  ");
        }

        // Print stars and hollow spaces in the current row
        for (int k = 0; k < 2 * (n - l) - 1; k++) {
            if (k == 0 || k == 2 * (n - l) - 2 || i == 0
                || i == 2 * n - 2)
                printf("%d ", k + 1);
			else
                printf("  ");
        }
        printf("\n");
    }

    return 0;
}
Alphabet Pattern
#include <stdio.h>

int main() {
    int n = 5;

    // Outer loop to iterate through each row
    for (int i = 0; i < 2 * n - 1; i++) {

        // Determine the row level relative to the center
        int l = (i < n) ? i : 2 * n - 2 - i;

        // Print leading spaces
        for (int j = 0; j < l; j++) {
            printf("  ");
        }

        // Print stars and hollow spaces in the current row
        for (int k = 0; k < 2 * (n - l) - 1; k++) {
            if (k == 0 || k == 2 * (n - l) - 2 || i == 0
                || i == 2 * n - 2)
                printf("%c ", k + 'A');
			else
                printf("  ");
        }
        printf("\n");
    }

    return 0;
}


Output

* * * * * * * * *   |   1 2 3 4 5 6 7 8 9    |   A B C D E F G H I
* * | 1 7 | A G
* * | 1 5 | A E
* * | 1 3 | A C
* | 1 | A
* * | 1 3 | A C
* * | 1 5 | A E
* * | 1 7 | A G
* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H I

Explanation:

  • The outer loop prints each row i.e. 2 * n - 1 rows.
  • The l variable determines the relative position of the current row from the top or bottom center:
    • Top half: l = i.
    • Bottom half: l = 2 * n - 2 - i.
  • The leading spaces are printed by the first inner loop according to the level
  • The top and bottom boundaries are printed by the second inner loop using condition: i == 0 || i == 2 * n - 2
  • The condition k == 0 || k == 2 * (n - l) - 2 ensures stars at the boundaries of each row for inner rows.



Similar Reads