Print an Alphabet Diamond Pattern in C

To print an alphabet diamond pattern in C, we use nested loops to generate spaces and characters in a structured manner. The pattern consists of an increasing sequence of characters forming the top half of the diamond, followed by a decreasing sequence for the bottom half. The middle row contains the widest sequence of characters.


Examples of Alphabet Diamond Patterns in C

1. Alphabet Diamond Pattern with a Fixed Height

In this example, we will generate an alphabet diamond pattern using uppercase letters, with the letter ‘A’ at the top and bottom. The middle row will contain the widest sequence of letters.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int i, j, k;
    char ch = 'A';
    int n = 5; // Number of rows in the upper half

    // Upper half of the diamond
    for (i = 0; i < n; i++) {
        for (j = i; j < n - 1; j++) {
            printf(" ");
        }
        for (k = 0; k <= i * 2; k++) {
            printf("%c", ch + k);
        }
        printf("\n");
    }

    // Lower half of the diamond
    for (i = n - 2; i >= 0; i--) {
        for (j = n - 1; j > i; j--) {
            printf(" ");
        }
        for (k = 0; k <= i * 2; k++) {
            printf("%c", ch + k);
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. We declare an integer variable n to define the number of rows in the upper half of the diamond.
  2. The first loop constructs the upper half of the diamond:
    • It prints spaces to align the letters.
    • It prints increasing alphabet characters from ‘A’.
  3. The second loop constructs the lower half of the diamond by decreasing the number of characters per row. Refer: C For Loop.

Output:

    A
   ABC
  ABCDE
 ABCDEFG
ABCDEFGHI
 ABCDEFG
  ABCDE
   ABC
    A

2. Alphabet Diamond Pattern Using User Input

In this example, we will allow the user to input the maximum letter, and the program will generate an alphabet diamond pattern dynamically.

main.c

</>
Copy
#include <stdio.h>

int main() {
    char maxChar;
    int i, j, k, n;

    printf("Enter the maximum letter (A-Z): ");
    scanf("%c", &maxChar);
    
    n = maxChar - 'A' + 1; // Convert character to numeric value

    // Upper half
    for (i = 0; i < n; i++) {
        for (j = i; j < n - 1; j++) {
            printf(" ");
        }
        for (k = 0; k <= i * 2; k++) {
            printf("%c", 'A' + k);
        }
        printf("\n");
    }

    // Lower half
    for (i = n - 2; i >= 0; i--) {
        for (j = n - 1; j > i; j--) {
            printf(" ");
        }
        for (k = 0; k <= i * 2; k++) {
            printf("%c", 'A' + k);
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. We take user input for the maximum letter and calculate the number of rows.
  2. We dynamically create the alphabet diamond based on user input.
  3. We adjust the number of spaces and characters in each row accordingly.

Output (for input ‘D’):

   A
  ABC
 ABCDE
ABCDEFG
 ABCDE
  ABC
   A

Conclusion

In this tutorial, we explored different methods to generate an alphabet diamond pattern in C:

  1. Fixed height alphabet diamond: Uses a predefined number of rows.
  2. User-defined alphabet diamond: Dynamically generates a pattern based on user input.