Print a Hollow Diamond Number Pattern in C

To print a hollow diamond number pattern in C, we use nested loops to create a symmetrical diamond shape. The numbers appear at the start and end of each row, while spaces fill the center. This pattern is typically divided into two halves: the upper half (increasing rows) and the lower half (decreasing rows).


Examples of Hollow Diamond Number Pattern

1. Basic Hollow Diamond Number Pattern

In this example, we will print a simple hollow diamond number pattern where the numbers are displayed at the beginning and end of each row, and spaces fill the middle.

main.c

</>
Copy
#include <stdio.h>

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

    // Upper part of the diamond
    for (int i = 1; i <= n; i++) {
        for (int j = i; j < n; j++) {
            printf(" "); // Printing spaces
        }
        printf("%d", i); // Print first number
        
        if (i > 1) {
            for (int j = 1; j < (2 * i - 2); j++) {
                printf(" "); // Printing spaces inside the diamond
            }
            printf("%d", i); // Print last number
        }
        printf("\n");
    }

    // Lower part of the diamond
    for (int i = n - 1; i >= 1; i--) {
        for (int j = n; j > i; j--) {
            printf(" "); // Printing spaces
        }
        printf("%d", i); // Print first number
        
        if (i > 1) {
            for (int j = 1; j < (2 * i - 2); j++) {
                printf(" "); // Printing spaces inside the diamond
            }
            printf("%d", i); // Print last number
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. We declare an integer n = 5 which determines the size of the pattern.
  2. The first loop prints the upper part of the diamond: Spaces are printed before the first number to center-align the pattern. The first number i is printed. Inner spaces are printed in between. The last number i is printed if i > 1.
  3. The second loop prints the lower part of the diamond using the same logic but in reverse order.
  4. The program ensures symmetrical alignment using space adjustments.

Output:

    1
   2 2
  3   3
 4     4
5       5
 4     4
  3   3
   2 2
    1

2. Hollow Diamond with Dynamic Input

In this example, we will take user input to decide the number of rows in the hollow diamond pattern.

main.c

</>
Copy
#include <stdio.h>

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

    // Upper part of the diamond
    for (int i = 1; i <= n; i++) {
        for (int j = i; j < n; j++) {
            printf(" ");
        }
        printf("%d", i);

        if (i > 1) {
            for (int j = 1; j < (2 * i - 2); j++) {
                printf(" ");
            }
            printf("%d", i);
        }
        printf("\n");
    }

    // Lower part of the diamond
    for (int i = n - 1; i >= 1; i--) {
        for (int j = n; j > i; j--) {
            printf(" ");
        }
        printf("%d", i);

        if (i > 1) {
            for (int j = 1; j < (2 * i - 2); j++) {
                printf(" ");
            }
            printf("%d", i);
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. The user is prompted to enter the number of rows (n).
  2. The pattern logic remains the same as the previous example, but the number of rows is now dynamic.
  3. scanf("%d", &n) captures user input for the pattern size.
  4. The loops adjust based on the user-defined n.

Example Output (for n = 4):

Enter the number of rows for half diamond: 4
   1
  2 2
 3   3
4     4
 3   3
  2 2
   1

Conclusion

In this tutorial, we learned how to print a hollow diamond number pattern in C. We covered:

  1. Printing a basic hollow diamond pattern using nested loops.
  2. Using user input to generate a dynamic hollow diamond pattern.