Left Half Pyramid
Program to print left half pyramid pattern
C++ Left 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++) {
// 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;
}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:
---
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
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):
rows - i = 5 - 1 = 4 spacesi = 1 star * (4 spaces + 1 star)Row 2 (i = 2):
5 - 2 = 3 spaces2 stars * * (3 spaces + 2 stars)Row 3 (i = 3):
5 - 3 = 2 spaces3 stars * * * (2 spaces + 3 stars)Row 4 (i = 4):
5 - 4 = 1 space4 stars * * * * (1 space + 4 stars)Row 5 (i = 5):
5 - 5 = 0 spaces5 stars* * * * * (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 spacesOuter Loop (`i`)
: Controls which row we're printing (1 to rows).
First Inner Loop
:
j <= rows - iSecond Inner Loop
:
j <= i`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
:
rows - ii3.
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
i from 1 to rows).(rows - i) spaces to align stars to the right.i stars in the current row.endl moves to the next line.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.