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 teaches you how to print various pyramid patterns in C++ using nested loops. Pyramid patterns are centered triangular shapes that demonstrate advanced loop control, spacing logic, and pattern formation. They are excellent exercises for understanding nested iterations and are commonly used in programming education and interviews.

---

1. What This Program Does

The program prints different pyramid patterns based on the number of rows entered by the user. For example, with 5 rows, it creates:

Full Pyramid: centered pyramid with increasing stars
Inverted Pyramid: upside-down pyramid with decreasing stars

Pyramid patterns require careful control of spaces (for centering) and stars (for the shape) to create symmetric triangular forms.

---

2. Header File Used

This header provides:

cout for displaying output
cin for taking input from the user

---

#include <iostream>

3. Understanding Pyramid Patterns

Key Concepts

:

Pyramids are centered triangular patterns
Require spaces before stars for centering
Number of stars follows odd sequence: 1, 3, 5, 7, 9, ...
Formula: 2*i - 1 stars in row i

Pattern Types

:

Full Pyramid: increasing from top (normal pyramid)
Inverted Pyramid: decreasing from top (upside-down)
Hollow Pyramid: stars only on edges
Number Pyramid: numbers instead of stars

---

4. Declaring Variables

The program declares:

int rows;

rows stores the number of rows entered by the user.
This determines the height of the pyramid.

---

5. Taking Input From the User

The program asks:

cin >> rows;

The user enters a number, for example: 5

---

cout << "Enter number of rows: ";

6. Pattern 1: Full Pyramid

for (int i = 1; i <= rows; i++) {

for (int j = 1; j <= rows - i; j++) {

}

for (int j = 1; j <= 2 * i - 1; j++) {

cout << "*";

}

cout << endl;

}

        cout << " ";

How it works

:

Outer loop (i): iterates from 1 to rows (each row)
First inner loop: prints (rows - i) spaces for centering
Second inner loop: prints (2*i - 1) stars (odd numbers)
Creates increasing centered pyramid

Step-by-step

(for rows = 5):

Row 1 (i=1):

Spaces: 5 - 1 = 4 spaces
Stars: 2*1 - 1 = 1 star
Output: " *"

Row 2 (i=2):

Spaces: 5 - 2 = 3 spaces
Stars: 2*2 - 1 = 3 stars
Output: " ***"

Row 3 (i=3):

Spaces: 5 - 3 = 2 spaces
Stars: 2*3 - 1 = 5 stars
Output: "

*"

And so on...

Output

(for rows = 5):

*

***

*

***

*

---

7. Pattern 2: Inverted Pyramid

for (int i = rows; i >= 1; i--) {

for (int j = 1; j <= rows - i; j++) {

}

for (int j = 1; j <= 2 * i - 1; j++) {

cout << "*";

}

cout << endl;

}

        cout << " ";

How it works

:

Outer loop (i): iterates from rows down to 1 (decreasing)
First inner loop: prints (rows - i) spaces (increasing as i decreases)
Second inner loop: prints (2*i - 1) stars (decreasing)
Creates decreasing centered pyramid

Output

(for rows = 5):

*

***

*

***

*

---

8. Understanding the Formulas

Spacing Formula: (rows - i)

:

Row 1: rows - 1 spaces (most spaces)
Row rows: rows - rows = 0 spaces (no spaces)
Creates centering effect

Star Formula: (2*i - 1)

:

Ensures odd number of stars per row
Row 1: 2*1 - 1 = 1 star
Row 2: 2*2 - 1 = 3 stars
Row 3: 2*3 - 1 = 5 stars
Creates pyramid shape

Why Odd Numbers?

:

Pyramids are symmetric around center
Odd numbers allow a center star
Even numbers would create asymmetric appearance

---

9. Other Patterns (Mentioned but not shown in code)

Hollow Pyramid

:

Print stars only at edges
Use conditional: if (j == 1 || j == 2*i-1 || i == rows)
Creates outline pyramid

Number Pyramid

:

Replace stars with numbers
Numbers can increase, decrease, or follow sequences
Example: 1, 2 3, 4 5 6, ...

Alphabet Pyramid

:

Use letters instead of stars
A, B C, D E F, ...

Floyd's Pyramid

:

Sequential numbers: 1, 2 3, 4 5 6, 7 8 9 10, ...

Pascal's Pyramid

:

Mathematical pattern with binomial coefficients
More complex pattern

---

10. When to Use Pyramid Patterns

Educational Purposes

:

Advanced nested loop practice
Understanding spacing and centering
Developing pattern recognition

Interview Preparation

:

Common coding interview problem
Tests understanding of loops and formulas
Demonstrates logical thinking

Visual Programming

:

Creating geometric shapes
ASCII art generation
Pattern-based graphics

---

11. Important Considerations

Spacing

:

Single space " " for centering
(rows - i) spaces decrease as row increases
Proper spacing creates symmetric appearance

Star Count

:

Formula 2*i - 1 ensures odd numbers
Critical for pyramid shape
Test with different row counts

Loop Direction

:

Full pyramid: increasing loop (1 to rows)
Inverted pyramid: decreasing loop (rows to 1)
Choose based on pattern direction

---

12. return 0;

This ends the program successfully.

---

Summary

Pyramid patterns use nested loops with spacing and star formulas.
Full Pyramid: increasing stars (2*i - 1) with decreasing spaces (rows - i).
Inverted Pyramid: decreasing stars with increasing spaces.
Formula 2*i - 1 ensures odd number of stars per row for symmetry.
Spacing formula (rows - i) centers each row properly.
Understanding formulas is essential for pyramid patterns.
Variations include hollow, number, alphabet pyramids.
Pyramid patterns demonstrate advanced nested loop mastery.

This program is fundamental for beginners learning advanced pattern printing, understanding spacing logic, and preparing for complex pattern problems in C++ programs.

Let us now understand every line and the components of the above program.

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.

Practical Learning Notes for Pyramid Program

This C++ program is part of the "Advanced Pattern Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents