Full Pyramid
Program to print full pyramid pattern
C++ Full 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 <= 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}Enter number of rows: 5
*
***
*****
*******
*********Understanding Full Pyramid
This program teaches you how to print a full pyramid pattern using nested loops in C++. A full pyramid is a centered triangular pattern where each row has an odd number of stars (1, 3, 5, 7, ...), creating a perfect triangle shape. This is more complex than half pyramids and helps beginners understand how to create centered patterns.
---
1. What is a Full Pyramid?
A full pyramid is a centered pattern that looks like this (for 5 rows):
*
***
*
***
*
Notice:
---
2. Header File
#include <iostream>
Provides cout for output and cin for input.
---
3. Variable Declaration
int rows;
Stores the number of rows in the pyramid.
---
4. Taking Input
cin >> rows;
Gets the number of rows from the user.
---
5. Understanding the Pattern Formula
For a full pyramid, we need to understand two formulas:
1.
Spaces
: rows - i spaces on the left (to center the stars)
2.
Stars
: 2 * i - 1 stars in each row
Why `2 * i - 1`?
2 * 1 - 1 = 1 star2 * 2 - 1 = 3 stars2 * 3 - 1 = 5 stars2 * 4 - 1 = 7 stars2 * 5 - 1 = 9 stars---
6. Step-by-Step Breakdown
Let's trace through when rows = 5:
Row 1 (i = 1):
rows - i = 5 - 1 = 4 spaces2 * i - 1 = 2 * 1 - 1 = 1 star * (4 spaces + 1 star)Row 2 (i = 2):
5 - 2 = 3 spaces2 * 2 - 1 = 3 stars *** (3 spaces + 3 stars)Row 3 (i = 3):
5 - 3 = 2 spaces2 * 3 - 1 = 5 stars*` (2 spaces + 5 stars)
Row 4 (i = 4):
5 - 4 = 1 space2 * 4 - 1 = 7 stars***` (1 space + 7 stars)
Row 5 (i = 5):
5 - 5 = 0 spaces2 * 5 - 1 = 9 stars*` (0 spaces + 9 stars)
---
7. Code Explanation
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
cout << " ";
}
// Print stars
for (int j = 1; j <= 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
// Print spacesOuter Loop (`i`)
: Controls the row number (1 to rows).
First Inner Loop
(Spaces):
j <= rows - ii increases, spaces decreaseSecond Inner Loop
(Stars):
j <= 2 * i - 1i increases, stars increase by 2`cout << endl;`
: Moves to the next line after each row.
---
8. Visual Representation
For `rows = 5`:
Row 1: [4 spaces] [1 star] → *
Row 2: [3 spaces] [3 stars] → ***
Row 3: [2 spaces] [5 stars] →
*
Row 4: [1 space] [7 stars] →
***
Row 5: [0 spaces] [9 stars] →
*
---
9. Why Odd Numbers of Stars?
Odd numbers (1, 3, 5, 7...) create a perfect centered triangle because:
If we used even numbers (2, 4, 6, 8...), the pattern wouldn't look as balanced.
---
10. Key Concepts Demonstrated
1.
Centered Patterns
: Using spaces to center content is a common technique in formatting output.
2.
Mathematical Formulas
: Understanding how to derive formulas (2 * i - 1) for patterns.
3.
Nested Loops
: Two inner loops working together to create complex patterns.
4.
Sequential Logic
: Spaces first, then stars, then newline.
---
11. Common Variations
Once you understand this pattern, you can create:
---
12. Common Mistakes
1.
Wrong star formula
: Using i instead of 2 * i - 1
2.
Wrong space formula
: Using i instead of rows - i
3.
Missing spaces
: Forgetting to print spaces before stars
4.
Extra spaces
: Printing spaces after stars (not needed for this pattern)
---
Summary
i from 1 to rows).(rows - i) spaces to center the stars.(2 * i - 1) stars (odd numbers: 1, 3, 5, 7...).endl moves to the next line.This program is essential for understanding centered patterns, mathematical formulas in loops, and creating visually appealing output. It's a stepping stone to more complex patterns like diamonds and advanced geometric shapes.
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 Full 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.