C++ program to print inverted half pyramid using numbers, stars, and alphabets
ADVERTISEMENTS
In this article, you will learn how to print the inverted half pyramid patterns using numbers, stars, and alphabets in the c++ programming language.
Examples
Enter the number of rows: 5
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
1 2 3 4
1 2 3
1 2
1
Enter the number of rows: 5
A A A A A
B B B B
C C C
D D
E
B B B B
C C C
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 half pyramid using numbers
// C++ program to print inverted half pyramid using numbers
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter the number of rows: ";
cin >> rows;
if (rows > 0) {
cout << endl;
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++)
cout << j << " ";
cout << endl;
}
}
return 0;
}
Output
Enter the number of rows: 6
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
2. C++ program to print inverted half pyramid using stars
// C++ program to print inverted half pyramid using stars
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter the number of rows: ";
cin >> rows;
if (rows > 0) {
cout << endl;
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++)
cout << "*" << " ";
cout << endl;
}
}
return 0;
}
Output
Enter the number of rows: 6
* * * * * *
* * * * *
* * * *
* * *
* *
*
3. C++ program to print inverted half pyramid using Alphabets
// C++ program to print inverted half pyramid using Alphabets
#include <iostream>
using namespace std;
int main() {
int rows;
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 j = 1; j <= i; j++)
cout << alphabet << " ";
alphabet++;
cout << endl;
}
}
return 0;
}
Output
Enter the number of rows: 6
A A A A A A
B B B B B
C C C C
D D D
E E
F
Explanation
In these programs, we have taken input 6
from the user of the pattern then this input passes to loop iterations.
By following perspective logic to print the inverted half pyramid patterns.