Print a Diamond Star Pattern in C

To print a diamond star pattern in C, we use nested loops to print spaces and stars in a structured manner. The pattern consists of two symmetrical halves: an upper triangle and a lower inverted triangle. The outer loop manages rows, while inner loops control spaces and stars.


Examples of Diamond Star Pattern

1. Basic Diamond Star Pattern

In this example, we will print a diamond pattern with a fixed height. The upper half increases the number of stars, and the lower half decreases them.

main.c

</>
Copy
#include <stdio.h>

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

    // Upper part of diamond
    for (int i = 1; i <= n; i++) {
        for (int j = i; j < n; j++) {
            printf(" ");
        }
        for (int j = 1; j <= (2*i - 1); j++) {
            printf("*");
        }
        printf("\n");
    }

    // Lower part of diamond
    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++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. We declare an integer n to set the diamond height.
  2. The first for loop prints the upper triangle:
    • Spaces decrease from n-1 to 0 as rows increase.
    • Stars increase from 1 to (2*i - 1) in each row.
  3. The second for loop prints the lower inverted triangle:
    • Spaces increase from 0 to n-1.
    • Stars decrease symmetrically to match the upper half.

Reference: C For Loop

Output:

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

2. Hollow Diamond Star Pattern

In this example, we modify the pattern to print a hollow diamond, leaving spaces inside and only printing stars at the edges.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int n = 5;

    // Upper part of hollow diamond
    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 part of hollow diamond
    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. The logic follows the same structure as the solid diamond pattern.
  2. The key change is in printing stars:
    • Stars are only printed at the first and last positions of each row.
    • Spaces are added in between to create the hollow effect.

Output:

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

Conclusion

In this tutorial, we learned how to print diamond star patterns in C using nested loops. We explored:

  1. Basic Diamond Pattern: A full star-filled diamond.
  2. Hollow Diamond Pattern: A diamond with only boundary stars.

These patterns demonstrate how nested loops help in printing structured outputs with spaces and symbols.