An inverted pyramid pattern contains stars arranged in decreasing order of columns from the first row to the last.
- The number of elements decreases with each row.
- Nested loops are used to control spaces and symbols in the pattern.
Illustration
Input: n = 4
Inverted Half Pyramid Using *
* * * *
* * *
* *
*
Inverted Half Pyramid Using Numbers
1 2 3 4
1 2 3
1 2
1
Inverted Full Pyramid Using *
* * * * * * *
* * * * *
* * *
*
Inverted Half Pyramid Using *
For an inverted half pyramid, the first row contains n stars, and each subsequent row contains one fewer star. The outer loop controls the rows, while the inner loop prints the required number of stars.
using namespace std;
#include <bits/stdc++.h>
#include <iostream>
int main()
{
int n = 4;
for (int i = n; i >= 1; --i) {
for (int j = 1; j <= i; ++j) {
cout << "* ";
}
cout << endl;
}
return 0;
}
Output
* * * * * * * * * *
Inverted Half Pyramid Using Numbers
In this pattern, each row starts from 1 and prints numbers up to the current row length. The number of elements decreases from n to 1.
using namespace std;
#include <bits/stdc++.h>
#include <iostream>
int main()
{
int n=4; // took a default value
for (int i = n; i >= 1; --i) { // loop for iterating
for (int j = 1; j <= i; ++j) { // loop for printing
cout << j << " ";
}
cout << endl;
}
return 0;
}
Output
1 2 3 4 1 2 3 1 2 1
Inverted Full Pyramid Using *
An inverted full pyramid is formed by printing spaces before the stars. For each row:
- The number of leading spaces increases.
- The number of stars decreases.
- The number of stars in each row is 2 * i - 1.
using namespace std;
#include <bits/stdc++.h>
#include <iostream>
int main()
{
int n=5;
for (int i = n; i >= 1; --i) {
for (int k = 0; k < n - i; ++k) {
cout << " ";
}
for (int j = i; j <= 2 * i - 1; ++j) {
cout << "* ";
}
for (int j = 0; j < i - 1; ++j) {
cout << "* ";
}
cout << endl;
}
return 0;
}
Output
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*