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
#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:
- We define an integer
rows
to control the height of the pyramid. - The outer
for
loop (i
) controls the number of rows, starting from 0 torows - 1
. Refer: C For Loop. - The inner
for
loop starts from ‘A’ and prints characters up to'A' + (rows - i - 1)
. - Each row prints one less character than the previous row.
- 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
#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:
- We declare
rows = 5
to set the number of rows. - The outer loop iterates through each row, decreasing the number of printed characters.
- The inner loop starts from ‘a’ and prints characters up to
'a' + (rows - i - 1)
. - Each row prints one character less than the previous row.
- 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
#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:
- We define
rows = 5
to set the number of rows. - The outer loop controls the rows and reduces the number of characters per row.
- A new
for
loop prints leading spaces for center alignment. - The inner
for
loop prints uppercase letters from ‘A’ to the required number. - 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:
- Uppercase Alphabet Pyramid: Prints an inverted pyramid using uppercase letters.
- Lowercase Alphabet Pyramid: Uses lowercase letters in the same format.
- 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.