Pyramid Program in C++

Full, inverted, and hollow centered pyramids in C++. Hollow uses 2*i-1 and a single-space gap.

IntermediateAdvanced Pattern ProgramsExample 4 of 6
pyramid-program.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int rows;
6
7 cout << "Enter number of rows: ";
8 cin >> rows;
9
10 // Full Pyramid
11 cout << "\nFull Pyramid:" << endl;
12 for (int i = 1; i <= rows; i++) {
13 for (int j = 1; j <= rows - i; j++) {
14 cout << " ";
15 }
16 for (int j = 1; j <= 2 * i - 1; j++) {
17 cout << "*";
18 }
19 cout << endl;
20 }
21
22 // Inverted Pyramid
23 cout << "\nInverted Pyramid:" << endl;
24 for (int i = rows; i >= 1; i--) {
25 for (int j = 1; j <= rows - i; j++) {
26 cout << " ";
27 }
28 for (int j = 1; j <= 2 * i - 1; j++) {
29 cout << "*";
30 }
31 cout << endl;
32 }
33
34 // Hollow Pyramid
35 cout << "\nHollow Pyramid:" << endl;
36 for (int i = 1; i <= rows; i++) {
37 for (int j = 1; j <= rows - i; j++) {
38 cout << " ";
39 }
40 for (int j = 1; j <= 2 * i - 1; j++) {
41 if (j == 1 || j == 2 * i - 1 || i == rows) {
42 cout << "*";
43 } else {
44 cout << " ";
45 }
46 }
47 cout << endl;
48 }
49
50 return 0;
51}

Output

Enter number of rows: 5

Full Pyramid:
    *
   ***
  *****
 *******
*********

Inverted Pyramid:
*********
 *******
  *****
   ***
    *

Hollow Pyramid:
    *
   * *
  *   *
 *     *
*********

What's going on

Three centered pyramids: full, inverted, hollow. Same 2 * i - 1 star count as Full Pyramid.

Full: i = 1..rows, pad rows - i with " ", then that many "*".

Inverted: i = rows..1, same inners. 9 stars first when rows is 5.

Hollow:

if (j == 1 || j == 2 * i - 1 || i == rows) cout << "*"; else cout << " ";

Left peak, right peak, and the entire base. Interior is a single space — stars here have no trailing space, so the gap is " ", not " ".

Walk 5, hollow:

i=1: one star (both edges are the same cell)
i=2: star, space, star, padded
i=5: nine stars

i == rows missing → the base is two corners. 2 * i without - 1 → even counts and it stops looking like a triangle.

Labels between blocks so the three dumps don't run together.