Right Half Pyramid
Program to print right half pyramid pattern
C++ Right Half Pyramid Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}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 outputcin → 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
cin >> rows;
The program asks the user how many rows they want, and stores the value in the rows variable.
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):
i = 1 (first row)j runs from 1 to 1 (since j <= i means j <= 1)*cout << endl; moves to next line*Row 2 (i = 2):
i = 2 (second row)j runs from 1 to 2j = 1: prints * j = 2: prints * * *Row 3 (i = 3):
j runs from 1 to 3* * *Row 4 (i = 4):
j runs from 1 to 4* * * *Row 5 (i = 5):
j runs from 1 to 5* * * * *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 numberj controls the number of stars in that row3.
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
---
9. Common Variations
Once you understand this pattern, you can create variations:
---
Summary
i) controls which row we're printing (from 1 to rows).j) prints i stars in row i.endl moves to the next line.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.