Hollow Diamond Star Pattern in C Language with Free Codes

Hollow Diamond Star Pattern

Hollow Diamond Star Pattern Consist of two triangular Patterns, one facing upwards, and second facing downwards, with a common middle row. However, in Hollow Diamond Star Patterns, inner spaces are empty which is creating a hollow effect. The stars arranged in that way each row has a decreasing number of stars as it moves away from the center, with spaces filling the gap.

Output

Enter the number of rows (odd number): 7
   *
  * *
 *   *
*     *
 *   *
  * *
   *
Free Hand-Written Notes

Codes – Hollow Diamond Star Pattern

Here is the complete codes of Hollow Diamond Star Pattern,

#include <stdio.h>

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

    printf("Enter the number of rows (odd number): ");
    scanf("%d", &rows);

    // Upper part of the diamond
    for (i = 1; i <= rows / 2 + 1; i++) {
        for (space = 1; space <= rows / 2 + 1 - i; space++) {
            printf(" ");
        }
        for (j = 1; j <= 2 * i - 1; j++) {
            if (j == 1 || j == 2 * i - 1) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    // Lower part of the diamond
    for (i = rows / 2; i >= 1; i--) {
        for (space = 1; space <= rows / 2 + 1 - i; space++) {
            printf(" ");
        }
        for (j = 1; j <= 2 * i - 1; j++) {
            if (j == 1 || j == 2 * i - 1) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}

Codes Explanation

  1. Here the program shows the prompts to user for enter the number of rows for the hollow diamond (an odd number).
  2. Then it divides the diamond into two parts: the upper part and the lower part.
  3. For each part, it iterates through each row using a for loop.
  4. Within each row, it prints leading spaces to center-align the stars.
  5. It then prints the stars or spaces using another nested for loop.
  6. Stars are printed only at the beginning and end of each row, leaving the inner spaces empty.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top