Left Half Pyramid

Program to print left half pyramid pattern

C++Beginner
C++
#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
        *
      * *
    * * *
  * * * *
* * * * *

Left Half Pyramid in C++

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.

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

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.

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.