Print an Alphabet Hollow Diamond in C

To print an alphabet hollow diamond pattern in C, we use nested loops to print spaces and characters in a structured way. The pattern consists of an upper and lower half where the outermost characters form an alphabet shape, while the inner part remains hollow. This tutorial covers different methods to achieve this pattern with detailed explanations and examples.


Examples to Print an Alphabet Hollow Diamond

1. Basic Alphabet Hollow Diamond Using Nested Loops

In this example, we will use nested loops to print a hollow diamond pattern with letters starting from ‘A’ at the top and bottom. The diamond structure is formed using spaces and characters.

main.c

</>
Copy
#include <stdio.h>

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

    // Upper half of the diamond
    for (int i = 1; i <= n; i++) {
        ch = 'A' + i - 1; // Assign letter based on row index

        // Printing leading spaces
        for (int j = 1; j <= n - i; j++) {
            printf(" ");
        }

        // Printing characters
        for (int j = 1; j <= (2 * i - 1); j++) {
            if (j == 1 || j == (2 * i - 1)) {
                printf("%c", ch);
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    // Lower half of the diamond
    for (int i = n - 1; i >= 1; i--) {
        ch = 'A' + i - 1;

        // Printing leading spaces
        for (int j = 1; j <= n - i; j++) {
            printf(" ");
        }

        // Printing characters
        for (int j = 1; j <= (2 * i - 1); j++) {
            if (j == 1 || j == (2 * i - 1)) {
                printf("%c", ch);
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. The variable n defines the number of rows in the upper half of the diamond.
  2. The upper half is constructed using a loop where spaces are printed first, followed by the letters.
  3. The ch variable stores the alphabet character corresponding to each row.
  4. The lower half mirrors the upper half by decrementing the loop variable.

Output:

    A    
   B B   
  C   C  
 D     D 
E       E
 D     D 
  C   C  
   B B   
    A    

2. Alphabet Hollow Diamond Using Functions

In this example, we break down the logic into functions for better reusability and clarity. We define separate functions to print spaces and characters.

main.c

</>
Copy
#include <stdio.h>

// Function to print leading spaces
void printSpaces(int count) {
    for (int i = 0; i < count; i++) {
        printf(" ");
    }
}

// Function to print hollow alphabet row
void printAlphabetRow(int row, int maxRows) {
    char ch = 'A' + row - 1;
    printf("%c", ch);

    if (row > 1) {
        printSpaces(2 * row - 3);
        printf("%c", ch);
    }
    printf("\n");
}

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

    // Upper half of the diamond
    for (int i = 1; i <= n; i++) {
        printSpaces(n - i);
        printAlphabetRow(i, n);
    }

    // Lower half of the diamond
    for (int i = n - 1; i >= 1; i--) {
        printSpaces(n - i);
        printAlphabetRow(i, n);
    }

    return 0;
}

Explanation:

  1. printSpaces() is a helper function that prints leading spaces.
  2. printAlphabetRow() prints the alphabet character and spaces inside the diamond.
  3. We use two loops in main() to handle the upper and lower halves separately.
  4. This approach improves code reusability and readability.

Output:

    A    
   B B   
  C   C  
 D     D 
E       E
 D     D 
  C   C  
   B B   
    A    

Conclusion

In this tutorial, we explored different ways to print an alphabet hollow diamond in C:

  1. Basic Approach: Using nested loops to print spaces and characters.
  2. Function-Based Approach: Breaking down logic into reusable functions.

Understanding nested loops and functions in C is key to working with patterns effectively. Try modifying the values to create different sizes of the hollow diamond pattern.