Left Half Pyramid

Program to print left half pyramid pattern

BeginnerTopic: Pattern Programs
Back

C++ Left Half 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 <= i; j++) {
            cout << "* ";
        }
        cout << endl;
    }
    
    return 0;
}
Output
Enter number of rows: 5
        *
      * *
    * * *
  * * * *
* * * * *

Understanding Left Half Pyramid

This program teaches you how to print a left half pyramid pattern using nested loops in C++. A left half pyramid is a pattern where stars are aligned to the right side, with spaces on the left. This pattern helps beginners understand how to combine spaces and characters to create aligned patterns, which is essential for creating professional-looking output.

---

1. What is a Left Half Pyramid?

A left half pyramid is a pattern that looks like this (for 5 rows):

        *
      * *
    * * *
  * * * *
* * * * *

Notice that:

The stars are aligned to the right
There are spaces on the left side
Each row has one more star than the previous row

---

2. Header File

#include <iostream>

This header allows the program to use cout for output and cin for input.

---

3. Variable Declaration

int rows;

Stores the number of rows the user wants in the pyramid.

---

4. Taking Input From the User

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

cin >> rows;

The program asks the user for the number of rows and stores it in rows.

---

5. Understanding the Pattern Logic

This pattern requires

two inner loops

:

1.

First inner loop

: Prints spaces (to push stars to the right)

2.

Second inner loop

: Prints stars

The key insight is:

For row i, we need (rows - i) spaces, then i stars.

---

6. Step-by-Step Breakdown

Let's trace through when rows = 5:

Row 1 (i = 1):

Spaces needed: rows - i = 5 - 1 = 4 spaces
Stars needed: i = 1 star
First loop: prints 4 spaces
Second loop: prints 1 star
Output: * (4 spaces + 1 star)

Row 2 (i = 2):

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

Row 3 (i = 3):

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

Row 4 (i = 4):

Spaces: 5 - 4 = 1 space
Stars: 4 stars
Output: * * * * (1 space + 4 stars)

Row 5 (i = 5):

Spaces: 5 - 5 = 0 spaces
Stars: 5 stars
Output: * * * * * (0 spaces + 5 stars)

---

7. Code Explanation

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

        cout << "  ";  // Two spaces for better alignment
    }

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

        cout << "* ";
    }

    cout << endl;
}
    // Print spaces

Outer Loop (`i`)

: Controls which row we're printing (1 to rows).

First Inner Loop

:

Condition: j <= rows - i
Purpose: Print spaces to push stars to the right
Example: Row 1 needs 4 spaces, so loop runs 4 times

Second Inner Loop

:

Condition: j <= i
Purpose: Print stars in the current row
Example: Row 1 needs 1 star, so loop runs 1 time

`cout << endl;`

: Moves to the next line after printing spaces and stars.

---

8. Why Two Spaces (`" "`)?

The code uses " " (two spaces) instead of " " (one space) for better visual alignment. This creates a more balanced pyramid appearance. You can adjust this based on your preference.

---

9. Key Concepts Demonstrated

1.

Multiple Nested Loops

: Using two inner loops within one outer loop to print different characters (spaces and stars).

2.

Pattern Formula

:

Spaces: rows - i
Stars: i

3.

Alignment

: Spaces are used to align the pattern to the right, creating a professional look.

4.

Sequential Execution

: The first loop completes (all spaces), then the second loop runs (all stars), then we move to the next row.

---

10. Visual Representation

For `rows = 5`:

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

---

11. Common Mistakes to Avoid

1.

Wrong space calculation

: Using i instead of rows - i for spaces

2.

Missing endl

: Forgetting cout << endl; after each row

3.

Loop order

: Printing stars before spaces (would create left-aligned pattern)

---

Summary

The outer loop controls the row number (i from 1 to rows).
The first inner loop prints (rows - i) spaces to align stars to the right.
The second inner loop prints i stars in the current row.
After each row, endl moves to the next line.
This creates a left half pyramid where stars are right-aligned with spaces on the left.

This pattern is essential for understanding alignment, nested loops, and creating visually appealing output 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 Left Half 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