Right Half Pyramid

Program to print right 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++) {
        for (int j = 1; j <= i; j++) {
            cout << "* ";
        }
        cout << endl;
    }
    
    return 0;
}

Output

Enter number of rows: 5
*
* *
* * *
* * * *
* * * * *

Right Half Pyramid in C++

This program teaches you how to print a right half pyramid pattern using nested loops in C++. A right half pyramid is a pattern where each row contains one more star than the previous row, creating a triangular shape that is aligned to the left side. This is one of the most fundamental pattern printing programs and helps beginners understand nested loops, which are essential for many programming problems.

What is a Right Half Pyramid?

A right half pyramid is a pattern that looks like this:

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

Each row has one more star than the row above it, starting with 1 star in the first row.

Understanding Nested Loops

This program uses ## nested loops — a loop inside another loop. This is a very important concept in programming.

  • Outer loop (for (int i = 1; i <= rows; i++)) → controls which row we are printing

  • Inner loop (for (int j = 1; j <= i; j++)) → controls how many stars to print in the current row

Pattern Formula

In row i, we print i stars. This is why the inner loop condition is j <= i.

Step-by-step (for rows = 5):

  • Row 1 (i = 1): Print 1 star

  • Row 2 (i = 2): Print 2 stars

  • Row 3 (i = 3): Print 3 stars

  • Row 4 (i = 4): Print 4 stars

  • Row 5 (i = 5): Print 5 stars

Summary

  • The outer loop (i) controls which row we're printing (from 1 to rows).
  • The inner loop (j) prints i stars in row i.
  • After each row, endl moves to the next line.
  • This creates a right-aligned pyramid pattern where each row has one more star than the previous row.

This program is essential for beginners to master nested loops and pattern printing, which are building blocks for more advanced programming concepts.