C++ Program to Print Pyramid Pattern
You can print a pyramid pattern in C++ using nested
for loops where the outer loop controls the rows and the inner loops print spaces and stars; for example, for (int i = 1; i <= n; ++i) { for (int j = n - i; j > 0; --j) cout << ' '; for (int k = 1; k <= 2 * i - 1; ++k) cout << '*'; cout << '\n'; } prints a pyramid of height n.Examples
Input3
Output *
***
*****
Input5
Output *
***
*****
*******
*********
Input1
Output*
How to Think About It
To print a pyramid pattern, think of each row as having some spaces followed by stars. The number of spaces decreases as you go down, and the number of stars increases. Use one loop to go through each row, then inside it, print spaces first, then stars.
Algorithm
1
Get the number of rows (height) from the user.2
For each row from 1 to height:3
Print (height - current row) spaces.4
Print (2 * current row - 1) stars.5
Move to the next line.Code
cpp
#include <iostream> using namespace std; int main() { int n; cout << "Enter pyramid height: "; cin >> n; for (int i = 1; i <= n; ++i) { for (int j = n - i; j > 0; --j) cout << ' '; for (int k = 1; k <= 2 * i - 1; ++k) cout << '*'; cout << '\n'; } return 0; }
Output
Enter pyramid height: 5
*
***
*****
*******
*********
Dry Run
Let's trace the pyramid pattern for input 3 through the code
1
Input
User enters n = 3
2
Row 1
Print 2 spaces, then 1 star: ' *'
3
Row 2
Print 1 space, then 3 stars: ' ***'
4
Row 3
Print 0 spaces, then 5 stars: '*****'
| Row (i) | Spaces (n - i) | Stars (2*i - 1) |
|---|---|---|
| 1 | 2 | 1 |
| 2 | 1 | 3 |
| 3 | 0 | 5 |
Why This Works
Step 1: Outer loop controls rows
The for loop with variable i runs from 1 to n, each iteration prints one row.
Step 2: Print spaces first
For each row, print n - i spaces to align stars in pyramid shape.
Step 3: Print stars next
Then print 2 * i - 1 stars to form the pyramid's width for that row.
Alternative Approaches
Using while loops
cpp
#include <iostream> using namespace std; int main() { int n, i = 1; cout << "Enter pyramid height: "; cin >> n; while (i <= n) { int spaces = n - i; while (spaces > 0) { cout << ' '; spaces--; } int stars = 2 * i - 1; while (stars > 0) { cout << '*'; stars--; } cout << '\n'; i++; } return 0; }
This uses while loops instead of for loops, which some beginners find easier to understand but is slightly longer.
Using recursion
cpp
#include <iostream> using namespace std; void printPyramid(int n, int i = 1) { if (i > n) return; for (int j = n - i; j > 0; --j) cout << ' '; for (int k = 1; k <= 2 * i - 1; ++k) cout << '*'; cout << '\n'; printPyramid(n, i + 1); } int main() { int n; cout << "Enter pyramid height: "; cin >> n; printPyramid(n); return 0; }
This recursive approach prints the pyramid by calling the function for each row, which is elegant but less common for beginners.
Complexity: O(n^2) time, O(1) space
Time Complexity
The program uses nested loops: the outer loop runs n times, and the inner loops run up to 2*n times in total per row, resulting in O(n^2) time.
Space Complexity
Only a few variables are used; no extra data structures, so space complexity is O(1).
Which Approach is Fastest?
All approaches have similar time complexity; iterative for-loop method is simplest and most readable.
| Approach | Time | Space | Best For |
|---|---|---|---|
| For loops | O(n^2) | O(1) | Simplicity and readability |
| While loops | O(n^2) | O(1) | Beginners preferring while syntax |
| Recursion | O(n^2) | O(n) due to call stack | Demonstrating recursion concepts |
Use nested loops: one for spaces and one for stars to easily build the pyramid shape.
Beginners often forget to print the correct number of spaces, causing the pyramid to be misaligned.