Pyramid Program

Pyramid Program in C++ (10 Easy Patterns with Code & Output)

IntermediateTopic: Advanced Pattern Programs
Back

C++ Pyramid Program Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
using namespace std;

int main() {
    int rows;
    
    cout << "Enter number of rows: ";
    cin >> rows;
    
    // Full Pyramid
    cout << "\nFull Pyramid:" << endl;
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= rows - i; j++) {
            cout << " ";
        }
        for (int j = 1; j <= 2 * i - 1; j++) {
            cout << "*";
        }
        cout << endl;
    }
    
    // Inverted Pyramid
    cout << "\nInverted Pyramid:" << endl;
    for (int i = rows; i >= 1; i--) {
        for (int j = 1; j <= rows - i; j++) {
            cout << " ";
        }
        for (int j = 1; j <= 2 * i - 1; j++) {
            cout << "*";
        }
        cout << endl;
    }
    
    // Hollow Pyramid
    cout << "\nHollow Pyramid:" << endl;
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= rows - i; j++) {
            cout << " ";
        }
        for (int j = 1; j <= 2 * i - 1; j++) {
            if (j == 1 || j == 2 * i - 1 || i == rows) {
                cout << "*";
            } else {
                cout << " ";
            }
        }
        cout << endl;
    }
    
    return 0;
}
Output
Enter number of rows: 5

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

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

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

Understanding Pyramid Program

This program demonstrates 10 different pyramid patterns: full pyramid, inverted pyramid, hollow pyramid, number pyramid, alphabet pyramid, Floyd's pyramid, Pascal's pyramid, and various combinations.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Table of Contents