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.
Similar Reads
C Program To Print Inverted Hollow Pyramid Patterns In C, hollow inverted pyramids are pyramid patterns that are flipped 180° vertically with the characters present only at the edge while the inside remains empty. In this article, we will learn how to print different hollow inverted pyramid patterns using C programs.There are three common inverted ho
6 min read
C Program For Printing Right Half Pyramid Pattern A half-right pyramid consists of rows with sequential stars, numbers or characters arranged in a triangular shape. The first row has one character, the second row has two, and so on. The characters are aligned to the left making it similar to the right-angle triangle. In this article, we will learn
5 min read
C Program to Print Pyramid Pattern In C, a pyramid pattern consists of numbers, stars, or alphabets arranged in a triangular shape. In this article, we will learn how to print different shapes of pyramid patterns using C program.Following are the 6 common pyramid patterns:Right Half Pyramid PatternRight half pyramid pattern looks lik
13 min read
C Program To Print Character Pyramid Pattern 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 PatternHalf
4 min read
C Program To Print Hollow Pyramid Patterns The Hollow Pyramid patterns are the variation of pyramid patterns where only the outer edges are filled with characters but the interior is left empty. In this article, we will learn how to print different hollow pyramid patterns.There can be 5 hollow pyramid patterns corresponding to each of the no
12 min read
C Program to Print Right Half Pyramid Pattern The Right Half Pyramid Pattern is a triangular pattern consists of rows where each row contains an increasing number of characters. The number of characters starts from 1 and increases by 1 in each subsequent row. Characters are aligned to the left, resembling a right-angle triangle with its hypoten
2 min read
C Program to Print Floyd's Triangle Pyramid Patterns Floydâs triangle pyramid pattern is a pattern where numbers or characters are printed in a triangular shape, with each row containing an increasing number of consecutive integers or characters aligned to the left forming a shape similar to right half pyramid pattern.Floyd's Triangle Pyramid Patterns
2 min read
C Program To Print Continuous Character Pyramid Pattern There are 2 ways to print continuous character patterns in C i.e: Using for loopUsing while loop Input: rows = 5 Output: A B C D E F G H I J K L M N O1. Using for loopApproach 1: Using CharacterAssign any character to one variable for the printing pattern. The first for loop is used to iterate the n
5 min read
C Program to Print 180 Degree Rotation of Inverted Right Half Pyramid The 180° rotation of a simple half left pyramid pattern leads to the pattern whose base is aligned with the bottom and characters are aligned to the left and increase in count while going down. In this article, we will learn how to print the 180° Rotated Half Left Pyramid using a C program. The belo
2 min read
C Program For Printing 180 Degree Rotation of Simple Half Left Pyramid The 180° Rotation of a Simple Half Left Pyramid is a pattern where the base of the pyramid is aligned with the bottom, and the entire structure appears rotated by 180°. The resulting structure is nothing but inverted right half pyramid. In this article, we will learn how to print the 180° Rotated Ha
2 min read