Print an Inverted Alphabet Pyramid in C

To print an inverted alphabet pyramid in C, we use nested loops to control the rows and columns. The outer loop manages the number of rows, while the inner loop prints characters in decreasing order. By adjusting the ASCII values of characters, we can create different pyramid patterns.


Examples of Inverted Alphabet Pyramid

1. Inverted Alphabet Pyramid Using Uppercase Letters

In this example, we will print an inverted alphabet pyramid using uppercase letters from ‘A’ to ‘Z’. The first row contains all letters up to a given number, and each subsequent row decreases by one character.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int rows = 5; // Number of rows in the pyramid
    
    for (int i = 0; i < rows; i++) {
        for (char ch = 'A'; ch <= 'A' + (rows - i - 1); ch++) {
            printf("%c ", ch);
        }
        printf("\n");
    }
    
    return 0;
}

Explanation:

  1. We define an integer rows to control the height of the pyramid.
  2. The outer for loop (i) controls the number of rows, starting from 0 to rows - 1. Refer: C For Loop.
  3. The inner for loop starts from ‘A’ and prints characters up to 'A' + (rows - i - 1).
  4. Each row prints one less character than the previous row.
  5. After printing each row, printf("\n") moves to the next line.

Output:

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

2. Inverted Alphabet Pyramid Using Lowercase Letters

In this example, we will print an inverted alphabet pyramid using lowercase letters. The structure is similar to the previous example, but we use lowercase letters from ‘a’ to ‘z’.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int rows = 5;

    for (int i = 0; i < rows; i++) {
        for (char ch = 'a'; ch <= 'a' + (rows - i - 1); ch++) {
            printf("%c ", ch);
        }
        printf("\n");
    }
    
    return 0;
}

Explanation:

  1. We declare rows = 5 to set the number of rows.
  2. The outer loop iterates through each row, decreasing the number of printed characters.
  3. The inner loop starts from ‘a’ and prints characters up to 'a' + (rows - i - 1).
  4. Each row prints one character less than the previous row.
  5. The printf("\n") moves to the next line after printing each row.

Output:

a b c d e 
a b c d 
a b c 
a b 
a

3. Inverted Alphabet Pyramid with Centered Alignment

In this example, we print an inverted alphabet pyramid but align it to the center using spaces.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int rows = 5;

    for (int i = 0; i < rows; i++) {
        for (int space = 0; space < i; space++) {
            printf("  "); // Print leading spaces
        }
        for (char ch = 'A'; ch <= 'A' + (rows - i - 1); ch++) {
            printf("%c ", ch);
        }
        printf("\n");
    }
    
    return 0;
}

Explanation:

  1. We define rows = 5 to set the number of rows.
  2. The outer loop controls the rows and reduces the number of characters per row.
  3. A new for loop prints leading spaces for center alignment.
  4. The inner for loop prints uppercase letters from ‘A’ to the required number.
  5. Each row prints one less character than the previous row.

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 inverted alphabet pyramid in C:

  1. Uppercase Alphabet Pyramid: Prints an inverted pyramid using uppercase letters.
  2. Lowercase Alphabet Pyramid: Uses lowercase letters in the same format.
  3. Centered Alphabet Pyramid: Aligns the pyramid to the center using spaces.

These patterns are useful for understanding loops, nested loops, and character printing in C.