C++
#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;
}Output
Enter number of rows: 5
*
***
*****
*******
*********Full Pyramid in C++
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.
What is a Full Pyramid?
A full pyramid is a centered pattern that looks like this (for 5 rows):
*
***
## *
## ***
## ## *
Notice:
- Stars are centered
- Each row has an odd number of stars: 1, 3, 5, 7, 9...
- Spaces on both sides create the centered effect
- The pattern forms a perfect triangle
Understanding the Pattern Formula
For a full pyramid, we need to understand two formulas:
-
Spaces:
rows - ispaces on the left (to center the stars) -
Stars:
2 * i - 1stars in each row
Why 2 * i - 1?
- Row 1:
2 * 1 - 1 = 1star - Row 2:
2 * 2 - 1 = 3stars - Row 3:
2 * 3 - 1 = 5stars - Row 4:
2 * 4 - 1 = 7stars - Row 5:
2 * 5 - 1 = 9stars
Summary
- Outer loop controls row number (
ifrom 1 to rows) - First inner loop prints
(rows - i)spaces for centering - Second inner loop prints
(2 * i - 1)stars - This creates a centered, full pyramid pattern
This program teaches advanced pattern printing techniques and is essential for understanding how to create visually appealing, centered patterns.