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:
- We declare integer variables
i
andj
for loop control, and a character variablech
to store the letters. - The outer
for
loop runs 5 times (for 5 rows), and in each iteration,ch
is reset to ‘A’. Refer C For Loop. - The inner
for
loop prints characters from ‘A’ to the corresponding letter in the row, incrementingch
after each print. 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:
- We declare variables
i
,j
, andspace
for loop control, andch
to store the characters. - The
rows
variable determines the number of rows in the pyramid. - The first
for
loop prints spaces to center-align the pyramid. - The second
for
loop prints characters from ‘A’ to the corresponding row letter. 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:
- We declare integer variables
i
andj
, and a character variablech
. - The outer loop starts from 5 and decrements down to 1, controlling the number of rows.
- The inner loop prints characters from ‘A’ to the corresponding row letter.
- The loop decreases in size each iteration, creating a reverse pyramid.
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:
- Simple Alphabet Pyramid: Prints increasing letters from ‘A’.
- Center-Aligned Pyramid: Prints a pyramid shape with spaces for alignment.
- Reverse Alphabet Pyramid: Prints letters in decreasing order.