Pyramid patterns is a classic logical programming exercise where a triangular looking pattern is printed by treating the output screen as a matrix and printing a given character. In this article, we will explore how to print various alphabet pyramid patterns using C program.
Half Pyramid Pattern
Half Pyramid Pattern is a triangular pattern where each row starts with 'A' and increases by one character per row. Characters are aligned to the left resembling a right-angled triangle with the hypotenuse facing right.
#include <stdio.h>
int main() {
int n = 5;
// Outer loop for printing rows
for (int i = 1; i <= n; i++) {
// Inner loop for printing alphabets in each row
for (int j = 1; j <= i; j++) {
printf("%c ", 'A' + j - 1);
}
printf("\n");
}
return 0;
}
Output
A A B A B C A B C D A B C D E
Inverted Half Pyramid Pattern
Inverted half pyramid pattern is nothing but the half pyramid pattern flipped vertically.
#include <stdio.h>
int main() {
int n = 5;
// Outer loop for printing rows
for (int i = n; i >= 1; i--) {
// Inner loop for printing
// the character in each row
for (int j = 0; j < i; j++)
printf("%c ", 'A' + j);
printf("\n");
}
return 0;
}
Output
A B C D E A B C D A B C A B A
Full Pyramid Pattern
The Full Pyramid Pattern looks like an equilateral triangle pattern where each row increases the number of characters by 2 from the top to the bottom row.
#include <stdio.h>
int main() {
int n = 5;
// Outer loop to print all rows
for (int i = 0; i < n; i++) {
// First inner loop 1 to print white spaces
for (int j = 0; j < 2 * (n - i) - 1; j++)
printf(" ");
// Second inner loop 2 to print alphabets
for (int k = 0; k < 2 * i + 1; k++)
printf("%c ", 'A' + k);
printf("\n");
}
return 0;
}
Output
A
A B C
A B C D E
A B C D E F G
A B C D E F G H I
Hollow Pyramid Pattern
The Hollow Pyramid Pattern forms a full pyramid where only the outer edges are filled with characters and the inner part of the pyramid is empty, creating a hollow space inside the shape.
#include <stdio.h>
int main() {
int n = 5;
// Outer loop to iterate through each row
for (int i = 0; i < n; i++) {
// First inner loop to print leading whitespaces
for (int j = 0; j < 2 * (n - i) - 1; j++)
printf(" ");
// Second inner loop to print alphabets and inner
// whitespaces
for (int k = 0; k < 2 * i + 1; k++) {
if (k == 0 || k == 2 * i || i == n - 1)
printf("%c ", k + 'A');
else
printf(" ");
}
printf("\n");
}
return 0;
}
Output
A
A C
A E
A G
A B C D E F G H I
Diamond Pyramid Pattern
The Diamond Pyramid Pattern can be considered as made up of two halves. The top half is a regular pyramid with increasing characters and the bottom half is an inverted pyramid with decreasing characters forming a diamond-like shape.
#include <stdio.h>
int main() {
int n = 5;
// Outer loop to iterate through each row
for (int i = 0; i < 2 * n - 1; i++) {
// Assigning values to the comp to distinguish
// between first half and second half
int comp;
if (i < n)
comp = 2 * (n - i) - 1;
else
comp = 2 * (i - n + 1) + 1;
// First inner loop to print leading whitespaces
for (int j = 0; j < comp; j++)
printf(" ");
// Second inner loop to print alphabet
for (int k = 0; k < 2 * n - comp; k++)
printf("%c ", k + 'A');
printf("\n");
}
return 0;
}
Output
A
A B C
A B C D E
A B C D E F G
A B C D E F G H I
A B C D E F G
A B C D E
A B C
A