Print an A to Z Alphabet Pyramid Pattern in C

To print an A to Z alphabet pyramid pattern in C, we use nested loops where the outer loop controls the rows, and the inner loop prints characters in a pyramid shape. The key to achieving this pattern is to understand ASCII values and character manipulation in C.


Examples to Print Alphabet Pyramid

1. Simple Alphabet Pyramid

In this example, we will print an alphabet pyramid where each row contains letters from ‘A’ to the corresponding row letter.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int i, j;
    char ch;

    // Outer loop controls the rows
    for (i = 0; i < 5; i++) {
        ch = 'A';
        
        // Inner loop prints the characters in pyramid pattern
        for (j = 0; j <= i; j++) {
            printf("%c ", ch);
            ch++;
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  1. We declare integer variables i and j for loop control, and a character variable ch to store the letters.
  2. The outer for loop runs 5 times (for 5 rows), and in each iteration, ch is reset to ‘A’. Refer C For Loop.
  3. The inner for loop prints characters from ‘A’ to the corresponding letter in the row, incrementing ch after each print.
  4. printf("\n") moves to the next row after printing all characters in the current row.

Output:

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

2. Center-Aligned Alphabet Pyramid

In this example, we will print an alphabet pyramid where each row is centered, creating a symmetrical pattern.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int i, j, space;
    char ch;

    // Number of rows
    int rows = 5;

    // Outer loop for rows
    for (i = 0; i < rows; i++) {

        // Print leading spaces for alignment
        for (space = 0; space < rows - i - 1; space++) {
            printf("  ");
        }

        ch = 'A';

        // Print characters in increasing order
        for (j = 0; j <= i; j++) {
            printf("%c ", ch);
            ch++;
        }

        printf("\n");
    }

    return 0;
}

Explanation:

  1. We declare variables i, j, and space for loop control, and ch to store the characters.
  2. The rows variable determines the number of rows in the pyramid.
  3. The first for loop prints spaces to center-align the pyramid.
  4. The second for loop prints characters from ‘A’ to the corresponding row letter.
  5. printf("\n") moves to the next row after printing characters.

Output:

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

3. Reverse Alphabet Pyramid

In this example, we will print an alphabet pyramid where the letters are printed in reverse order.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int i, j;
    char ch;

    // Outer loop for rows
    for (i = 5; i >= 1; i--) {
        ch = 'A';

        // Inner loop for characters
        for (j = 1; j <= i; j++) {
            printf("%c ", ch);
            ch++;
        }

        printf("\n");
    }

    return 0;
}

Explanation:

  1. We declare integer variables i and j, and a character variable ch.
  2. The outer loop starts from 5 and decrements down to 1, controlling the number of rows.
  3. The inner loop prints characters from ‘A’ to the corresponding row letter.
  4. The loop decreases in size each iteration, creating a reverse pyramid.
  5. printf("\n") moves to the next row after printing characters.

Output:

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

Conclusion

In this tutorial, we covered different ways to print an alphabet pyramid pattern in C:

  1. Simple Alphabet Pyramid: Prints increasing letters from ‘A’.
  2. Center-Aligned Pyramid: Prints a pyramid shape with spaces for alignment.
  3. Reverse Alphabet Pyramid: Prints letters in decreasing order.