Right Half Pyramid

Program to print right half pyramid pattern

BeginnerTopic: Pattern Programs
Back

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

Understanding Right Half Pyramid

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.

---

1. 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.

---

2. Header File

#include <iostream>

This header allows the program to use:

cout → for printing output
cin → for taking user input

---

3. Variable Declaration

int rows;

This variable stores the number of rows the user wants in the pyramid. The user will enter this value, and the program will print that many rows of stars.

---

4. Taking Input From the User

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

cin >> rows;

The program asks the user how many rows they want, and stores the value in the rows variable.

Example: If the user enters 5, then rows = 5, and the pyramid will have 5 rows.

---

5. 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

---

6. Step-by-Step Breakdown of the Pattern Logic

Let's trace through the program when rows = 5:

Row 1 (i = 1):

Outer loop: i = 1 (first row)
Inner loop: j runs from 1 to 1 (since j <= i means j <= 1)
Prints 1 star: *
After inner loop, cout << endl; moves to next line
Output: *

Row 2 (i = 2):

Outer loop: i = 2 (second row)
Inner loop: j runs from 1 to 2
j = 1: prints *
j = 2: prints *
After inner loop, moves to next line
Output: * *

Row 3 (i = 3):

Inner loop: j runs from 1 to 3
Prints 3 stars: * * *

Row 4 (i = 4):

Inner loop: j runs from 1 to 4
Prints 4 stars: * * * *

Row 5 (i = 5):

Inner loop: j runs from 1 to 5
Prints 5 stars: * * * * *

Final Output:

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

---

7. Key Concepts Demonstrated

1.

Nested Loops

: A loop inside another loop allows you to work with rows and columns (2D patterns).

2.

Loop Control Variables

:

i controls the row number
j controls the number of stars in that row

3.

Pattern Formula

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

4.

endl

: After printing each row, endl moves the cursor to the next line, creating the pyramid shape.

---

8. Why This Pattern is Important

Teaches nested loops, which are used in:
2D arrays
Matrix operations
More complex patterns
Game development (grids, boards)
Data visualization
Builds logical thinking: Understanding how to control rows and columns is fundamental to programming.

---

9. Common Variations

Once you understand this pattern, you can create variations:

Print numbers instead of stars
Print characters
Reverse the pattern (decreasing stars)
Add spaces for alignment

---

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.

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 Right 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