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++
cin
object - C++
cout
object
1. C++ program to print inverted pyramid pattern of Stars
// C++ program to print inverted pyramid pattern of Stars
#include <iostream>
using namespace std;
int main() {
int rows, j;
cout << "Enter the number of rows: ";
cin >> rows;
if (rows > 0) {
cout << endl;
for (int i = rows; i >= 1; i--) {
for (int leet = 0; leet < rows - i; leet++)
cout << " ";
for (j = i; j <= 2 * i - 1; j++)
cout << "* ";
for (j = 0; j < i - 1; j++)
cout << "* ";
cout << endl;
}
}
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 <iostream>
using namespace std;
int main() {
int rows, j, number = 1;
cout << "Enter the number of rows: ";
cin >> rows;
if (rows > 0) {
cout << endl;
for (int i = rows; i >= 1; i--) {
for (int leet = 0; leet < rows - i; leet++)
cout << " ";
for (j = i; j <= 2 * i - 1; j++) {
if (number > 10)
cout << number++ << " ";
else
cout << number++ << " ";
}
for (j = 0; j < i - 1; j++) {
if (number > 10)
cout << number++ << " ";
else
cout << number++ << " ";
}
cout << endl;
}
}
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 <iostream>
using namespace std;
int main() {
int rows, j;
char alphabet = 'A';
cout << "Enter the number of rows: ";
cin >> rows;
if (rows > 0) {
cout << endl;
for (int i = rows; i >= 1; i--) {
for (int leet = 0; leet < rows - i; leet++)
cout << " ";
for (j = i; j <= 2 * i - 1; j++)
cout << alphabet << " ";
for (j = 0; j < i - 1; j++)
cout << alphabet << " ";
alphabet++;
cout << endl;
}
}
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.