Print a Hollow Diamond Star Pattern in C

To print a hollow diamond star pattern in C, we use nested loops to control spaces and stars. The pattern consists of two parts: the upper half (increasing) and the lower half (decreasing). We use conditional statements inside the loop to print stars at the borders while leaving the inner area hollow.


Examples of Printing Hollow Diamond Star Pattern

1. Basic Hollow Diamond Star Pattern

In this example, we will print a basic hollow diamond star pattern with a given number of rows. We will use nested loops to print stars at the borders and spaces in the middle.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int n = 5; // Number of rows for the upper half

    // Upper half of the hollow diamond
    for (int i = 1; i <= n; i++) {
        for (int j = i; j < n; j++) {
            printf(" "); // Printing leading spaces
        }
        for (int j = 1; j <= (2 * i - 1); j++) {
            if (j == 1 || j == (2 * i - 1)) {
                printf("*"); // Printing border stars
            } else {
                printf(" "); // Printing hollow spaces
            }
        }
        printf("\n");
    }

    // Lower half of the hollow diamond
    for (int i = n - 1; i >= 1; i--) {
        for (int j = n; j > i; j--) {
            printf(" "); // Printing leading spaces
        }
        for (int j = 1; j <= (2 * i - 1); j++) {
            if (j == 1 || j == (2 * i - 1)) {
                printf("*"); // Printing border stars
            } else {
                printf(" "); // Printing hollow spaces
            }
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. We declare an integer n to control the size of the diamond.
  2. In the first loop, we print the upper half of the diamond:
    • Leading spaces are printed using for (int j = i; j < n; j++).
    • Stars are printed at the beginning and end of each row.
  3. The second loop prints the lower half, reversing the logic of the upper half.

Output:

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

2. Hollow Diamond with Adjustable Size

In this example, we modify the program to allow the user to input the diamond size dynamically.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int n;
    printf("Enter the number of rows for half diamond: ");
    scanf("%d", &n);

    // Upper half
    for (int i = 1; i <= n; i++) {
        for (int j = i; j < n; j++) {
            printf(" ");
        }
        for (int j = 1; j <= (2 * i - 1); j++) {
            if (j == 1 || j == (2 * i - 1)) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    // Lower half
    for (int i = n - 1; i >= 1; i--) {
        for (int j = n; j > i; j--) {
            printf(" ");
        }
        for (int j = 1; j <= (2 * i - 1); j++) {
            if (j == 1 || j == (2 * i - 1)) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. We declare an integer n and take user input for the number of rows.
  2. The scanf() function is used to read user input.
  3. The rest of the logic remains the same, but now the user can control the pattern size.

Output:

Enter the number of rows for half diamond: 4
   *
  * *
 *   *
*     *
 *   *
  * *
   *

Conclusion

In this tutorial, we learned how to print a hollow diamond star pattern in C:

  1. Basic Hollow Diamond: Using nested loops to print stars at borders.
  2. Dynamic Hollow Diamond: Allowing user input to control the size.