C Program For Printing Inverted Pyramid
Last Updated :
10 Dec, 2024
An Inverted Pyramids are inverted triangular patterns where the base is at the top, and the rows decrease in size as they move downwards. In this article, we will learn how to print different types of inverted pyramid patterns using C program.
There can be 3 types of inverted pyramid patterns:
Inverted Right Half Pyramid
The Inverted Right Half Pyramid Pattern is a triangular pattern where the largest row is at the top and the number of characters in a row decreases as we move downwards. Each row is aligned to the left, resembling inverted right-angle triangle with its hypotenuse in right direction.
Star Pattern
#include <stdio.h>
int main() {
int n = 5;
// Outer loop to print all rows
for (int i = 0; i < n; i++) {
// Inner loop to print the * in each row
for (int j = 0; j < n - i; j++)
printf("* ");
printf("\n");
}
}
Number Pattern
#include <stdio.h>
int main() {
int n = 5;
// Outer loop to print all rows
for (int i = 0; i < n; i++) {
// Inner loop to print the * in each row
for (int j = 0; j < n - i; j++)
printf("%d ", j + 1);
printf("\n");
}
}
Alphabet Pattern
#include <stdio.h>
int main() {
int n = 5;
// Outer loop to print all rows
for (int i = 0; i < n; i++) {
// Inner loop to print the * in each row
for (int j = 0; j < n - i; j++)
printf("%c ", j + 'A');
printf("\n");
}
}
Output
* * * * * | 1 2 3 4 5 | A B C D E
* * * * | 1 2 3 4 | A B C D
* * * | 1 2 3 | A B C
* * | 1 2 | A B
* | 1 | A
Explanation:
- Outer loop iterates over the rows starting from 0 to n - 1.
- Inner loop controls the number of stars (*) (or number/character) printed on each row. The number of stars printed decreases as i increases. Initially, when i = 0, it prints n stars, and when i = 1, it prints n - 1 stars, and so on.
Inverted Left Half Pyramid
The Inverted Left Half Pyramid Pattern is a triangular pattern similar to the right half pyramid, but the characters are aligned to the right instead of left.
Star Pattern
#include <stdio.h>
int main() {
int n = 5;
// First loop for printing all rows
for (int i = 0; i < n; i++) {
// First inner loop for printing white spaces
for (int j = 0; j < 2 * i; j++)
printf(" ");
// Second inner loop for printing star *
for (int k = 0; k < n - i; k++)
printf("* ");
printf("\n");
}
return 0;
}
Number Pattern
#include <stdio.h>
int main() {
int n = 5;
// First loop for printing all rows
for (int i = 0; i < n; i++) {
// First inner loop for printing white spaces
for (int j = 0; j < 2 * i; j++)
printf(" ");
// Second inner loop for printing star *
for (int k = 0; k < n - i; k++)
printf("%d ", k + 1);
printf("\n");
}
return 0;
}
Alphabet Pattern
#include <stdio.h>
int main() {
int n = 5;
// First loop for printing all rows
for (int i = 0; i < n; i++) {
// First inner loop for printing white spaces
for (int j = 0; j < 2 * i; j++)
printf(" ");
// Second inner loop for printing star *
for (int k = 0; k < n - i; k++)
printf("%c ", k + 'A');
printf("\n");
}
return 0;
}
Output
* * * * * | 1 2 3 4 5 | A B C D E
* * * * | 1 2 3 4 | A B C D
* * * | 1 2 3 | A B C
* * | 1 2 | A B
* | 1 | A
Explanation:
- The outer loop iterates over the rows, starting from i = 0 to i = n - 1.
- First inner loop controls the number of white spaces printed on each row which increases as
i
increases. - Second inner loop controls the number of stars (*) (or number/character) printed on each row which decreases as i increases.
Inverted Full Pyramid
The Inverted Full Pyramid Pattern is a variation of the pyramid pattern where the largest row is at the top, and the number of characters decreases as we move downward. It is basically the inverted equilateral triangle.
Star Pattern
#include <stdio.h>
int main() {
int n = 5;
// First loop for printing all rows
for (int i = 0; i < n; i++) {
// First inner loop for printing leading white
// spaces
for (int j = 0; j < 2 * i; j++) {
printf(" ");
}
// Second inner loop for printing stars *
for (int k = 0; k < 2 * (n - i) - 1; k++) {
printf("* ");
}
printf("\n");
}
}
Number Pattern
#include <stdio.h>
int main() {
int n = 5;
// First loop for printing all rows
for (int i = 0; i < n; i++) {
// First inner loop for printing leading white
// spaces
for (int j = 0; j < 2 * i; j++) {
printf(" ");
}
// Second inner loop for printing stars *
for (int k = 0; k < 2 * (n - i) - 1; k++) {
printf("%d ", k + 1);
}
printf("\n");
}
}
Alphabet Pattern
#include <stdio.h>
int main() {
int n = 5;
// First loop for printing all rows
for (int i = 0; i < n; i++) {
// First inner loop for printing leading white
// spaces
for (int j = 0; j < 2 * i; j++) {
printf(" ");
}
// Second inner loop for printing stars *
for (int k = 0; k < 2 * (n - i) - 1; k++) {
printf("%c ", k + 'A');
}
printf("\n");
}
}
Output
* * * * * * * * * | 1 2 3 4 5 6 7 8 9 | A B C D E F G H I
* * * * * * * | 1 2 3 4 5 6 7 | A B C D E F G
* * * * * | 1 2 3 4 5 | A B C D E
* * * | 1 2 3 | A B C
* | 1 | A
Explanation:
- The outer loop iterates over the rows, from i = 0 to i = n - 1.
- First inner loop prints leading white spaces on each row.The number of spaces increases as i increases, with 2 * i spaces on row i.
- Second inner loop prints stars (*) (or number/character) on each row. The number of stars decreases as i increases. Initially, it prints 2 * (n - i) - 1 stars, reducing with each row.
Explore
C Basics
Arrays & Strings
Pointers and Structures
Memory Management
File & Error Handling
Advanced Concepts