Full Pyramid

Program to print full pyramid pattern

IntermediateTopic: Pattern Programs
Back

C++ Full Pyramid 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;
    
    for (int i = 1; i <= rows; i++) {
        // Print spaces
        for (int j = 1; j <= rows - i; j++) {
            cout << " ";
        }
        // Print stars
        for (int j = 1; j <= 2 * i - 1; j++) {
            cout << "*";
        }
        cout << endl;
    }
    
    return 0;
}
Output
Enter number of rows: 5
    *
   ***
  *****
 *******
*********

Understanding Full Pyramid

This program teaches you how to print a full pyramid pattern using nested loops in C++. A full pyramid is a centered triangular pattern where each row has an odd number of stars (1, 3, 5, 7, ...), creating a perfect triangle shape. This is more complex than half pyramids and helps beginners understand how to create centered patterns.

---

1. What is a Full Pyramid?

A full pyramid is a centered pattern that looks like this (for 5 rows):

    *
   ***
*
***
*

Notice:

Stars are centered
Each row has an odd number of stars: 1, 3, 5, 7, 9...
Spaces on both sides create the centered effect
The pattern forms a perfect triangle

---

2. Header File

#include <iostream>

Provides cout for output and cin for input.

---

3. Variable Declaration

int rows;

Stores the number of rows in the pyramid.

---

4. Taking Input

`cout << "Enter number of rows: ";`

cin >> rows;

Gets the number of rows from the user.

---

5. Understanding the Pattern Formula

For a full pyramid, we need to understand two formulas:

1.

Spaces

: rows - i spaces on the left (to center the stars)

2.

Stars

: 2 * i - 1 stars in each row

Why `2 * i - 1`?

Row 1: 2 * 1 - 1 = 1 star
Row 2: 2 * 2 - 1 = 3 stars
Row 3: 2 * 3 - 1 = 5 stars
Row 4: 2 * 4 - 1 = 7 stars
Row 5: 2 * 5 - 1 = 9 stars
This gives us the odd sequence: 1, 3, 5, 7, 9...

---

6. Step-by-Step Breakdown

Let's trace through when rows = 5:

Row 1 (i = 1):

Spaces: rows - i = 5 - 1 = 4 spaces
Stars: 2 * i - 1 = 2 * 1 - 1 = 1 star
Output: * (4 spaces + 1 star)

Row 2 (i = 2):

Spaces: 5 - 2 = 3 spaces
Stars: 2 * 2 - 1 = 3 stars
Output: *** (3 spaces + 3 stars)

Row 3 (i = 3):

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

*` (2 spaces + 5 stars)

Row 4 (i = 4):

Spaces: 5 - 4 = 1 space
Stars: 2 * 4 - 1 = 7 stars
Output: `

***` (1 space + 7 stars)

Row 5 (i = 5):

Spaces: 5 - 5 = 0 spaces
Stars: 2 * 5 - 1 = 9 stars
Output: `

*` (0 spaces + 9 stars)

---

7. Code Explanation

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

        cout << " ";
    }

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

        cout << "*";
    }

    cout << endl;
}
    // Print spaces

Outer Loop (`i`)

: Controls the row number (1 to rows).

First Inner Loop

(Spaces):

Condition: j <= rows - i
Prints spaces on the left to center the stars
As i increases, spaces decrease

Second Inner Loop

(Stars):

Condition: j <= 2 * i - 1
Prints an odd number of stars
As i increases, stars increase by 2

`cout << endl;`

: Moves to the next line after each row.

---

8. Visual Representation

For `rows = 5`:

Row 1: [4 spaces] [1 star]   →     *
Row 2: [3 spaces] [3 stars]  →    ***
Row 3: [2 spaces] [5 stars]  →
*
Row 4: [1 space]  [7 stars]  →
***
Row 5: [0 spaces] [9 stars]  →
*

---

9. Why Odd Numbers of Stars?

Odd numbers (1, 3, 5, 7...) create a perfect centered triangle because:

The center star aligns vertically
Each row has a clear center point
Creates symmetry in the pattern

If we used even numbers (2, 4, 6, 8...), the pattern wouldn't look as balanced.

---

10. Key Concepts Demonstrated

1.

Centered Patterns

: Using spaces to center content is a common technique in formatting output.

2.

Mathematical Formulas

: Understanding how to derive formulas (2 * i - 1) for patterns.

3.

Nested Loops

: Two inner loops working together to create complex patterns.

4.

Sequential Logic

: Spaces first, then stars, then newline.

---

11. Common Variations

Once you understand this pattern, you can create:

Hollow pyramid (stars only on edges)
Number pyramid (numbers instead of stars)
Character pyramid (different characters)
Diamond pattern (full pyramid + inverted pyramid)

---

12. Common Mistakes

1.

Wrong star formula

: Using i instead of 2 * i - 1

2.

Wrong space formula

: Using i instead of rows - i

3.

Missing spaces

: Forgetting to print spaces before stars

4.

Extra spaces

: Printing spaces after stars (not needed for this pattern)

---

Summary

The outer loop controls the row number (i from 1 to rows).
The first inner loop prints (rows - i) spaces to center the stars.
The second inner loop prints (2 * i - 1) stars (odd numbers: 1, 3, 5, 7...).
After each row, endl moves to the next line.
This creates a centered full pyramid with perfect triangular symmetry.

This program is essential for understanding centered patterns, mathematical formulas in loops, and creating visually appealing output. It's a stepping stone to more complex patterns like diamonds and advanced geometric shapes.

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 Full Pyramid

This C++ program is part of the "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