C program to print inverted pyramid pattern of numbers, stars, and alphabets
In this article, you will learn how to print the inverted pyramid patterns of the numbers, stars, and alphabets in the c programming language.
Examples
* * * * * * *
* * * * *
* * *
*
B B B B B B B
C C C C C
D D D
E
You should have knowledge of the following topics in c programming to understand these programs:
- C
main()
function - C
for
loop statement - C
if
condition statement - C
increment
operator - C
printf()
function
1. C program to print inverted pyramid pattern of Stars
// C program to print inverted pyramid pattern of Stars
#include <stdio.h>
int main() {
int rows, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
if (rows > 0) {
printf("\n");
for (int i = rows; i >= 1; i--) {
for (int leet = 0; leet < rows - i; leet++)
printf(" ");
for (j = i; j <= 2 * i - 1; j++)
printf("* ");
for (j = 0; j < i - 1; j++)
printf("* ");
printf("\n");
}
}
return 0;
}
Output
Enter the number of rows: 5
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
2. C program to print inverted pyramid pattern of Numbers
// C program to print inverted pyramid pattern of Numbers
#include <stdio.h>
int main() {
int rows, j, number = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
if (rows > 0) {
printf("\n");
for (int i = rows; i >= 1; i--) {
for (int leet = 0; leet < rows - i; leet++)
printf(" ");
for (j = i; j <= 2 * i - 1; j++) {
if (number > 10)
printf("%d ", number++);
else
printf("%d %c", number++, ' ');
}
for (j = 0; j < i - 1; j++) {
if (number > 10)
printf("%d ", number++);
else
printf("%d %c", number++, ' ');
}
printf("\n");
}
}
return 0;
}
Output
Enter the number of rows: 5
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21
22 23 24
25
3. C program to print inverted pyramid pattern of Alphabets
// C program to print inverted pyramid pattern of Alphabets
#include <stdio.h>
int main() {
int rows, j, alphabet = 'A';
printf("Enter the number of rows: ");
scanf("%d", &rows);
if (rows > 0) {
printf("\n");
for (int i = rows; i >= 1; i--) {
for (int leet = 0; leet < rows - i; leet++)
printf(" ");
for (j = i; j <= 2 * i - 1; j++)
printf("%c ", alphabet);
for (j = 0; j < i - 1; j++)
printf("%c ", alphabet);
alphabet++;
printf("\n");
}
}
return 0;
}
Output
Enter the number of rows: 5
A A A A A A A A A
B B B B B B B
C C C C C
D D D
E
Explanation
In these programs, we have taken input 5
from the user of the pattern then this input passes to loop iterations.
By following perspective logic to print the inverted pyramid pattern.