#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
// Right Half Pyramid
cout << "\nRight Half Pyramid:" << endl;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
// Left Half Pyramid
cout << "\nLeft Half Pyramid:" << endl;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
cout << " ";
}
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
// Full Pyramid
cout << "\nFull Pyramid:" << endl;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
cout << " ";
}
for (int j = 1; j <= 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}Output
Enter number of rows: 5
Right Half Pyramid:
*
* *
* * *
* * * *
* * * * *
Left Half Pyramid:
*
* *
* * *
* * * *
* * * * *
Full Pyramid:
*
***
*****
*******
*********This program teaches you how to print various star patterns in C++ using nested loops. Star patterns are excellent exercises for understanding loop control, nested iterations, and pattern recognition. They help develop logical thinking and are commonly used in programming interviews and educational contexts.
1. What This Program Does
The program prints different star patterns based on the number of rows entered by the user. For example, with 5 rows, it creates:
- Right Half Pyramid: increasing stars per row
- Left Half Pyramid: right-aligned increasing stars
- Full Pyramid: centered pyramid of stars
Pattern printing involves controlling the number of spaces and stars printed in each row to create visual shapes.
2. Header File Used
#include <iostream>
This header provides:
- cout for displaying output
- cin for taking input from the user
3. Understanding Pattern Printing
Key Concepts:
- Outer loop controls rows (vertical direction)
- Inner loops control columns (horizontal direction)
- Spaces are printed before stars for alignment
- Number of stars increases or decreases based on row number
Pattern Types:
- Right Half Pyramid: stars increase from left
- Left Half Pyramid: stars increase from right (with spaces)
- Full Pyramid: centered stars forming a triangle
4. Declaring Variables
The program declares: int rows;
- rows stores the number of rows entered by the user.
- This determines the size of all patterns.
5. Taking Input From the User
The program asks: cout << "Enter number of rows: "; cin >> rows;
The user enters a number, for example: 5
6. Pattern 1: Right Half Pyramid
for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { cout << "* "; } cout << endl; }
How it works:
- Outer loop (i): iterates from 1 to rows (each iteration = one row)
- Inner loop (j): prints i stars in row i
- Row 1: 1 star, Row 2: 2 stars, Row 3: 3 stars, etc.
Output (for rows = 5):
7. Pattern 2: Left Half Pyramid
for (int i = 1; i <= rows; i++) { for (int j = 1; j <= rows - i; j++) { cout << " "; } for (int j = 1; j <= i; j++) { cout << "* "; } cout << endl; }
How it works:
- First inner loop: prints (rows - i) spaces for right alignment
- Second inner loop: prints i stars
- Spaces push stars to the right, creating left-aligned pyramid
Output (for rows = 5):
*
* *
* * *
8. Pattern 3: Full Pyramid
for (int i = 1; i <= rows; i++) { for (int j = 1; j <= rows - i; j++) { cout << " "; } for (int j = 1; j <= 2 * i - 1; j++) { cout << "*"; } cout << endl; }
How it works:
- First inner loop: prints (rows - i) spaces for centering
- Second inner loop: prints (2*i - 1) stars (odd numbers: 1, 3, 5, 7, ...)
- Formula 2*i - 1 ensures odd number of stars per row
Output (for rows = 5):
*
*
***
## *
Why 2*i - 1?:
- Row 1: 2*1 - 1 = 1 star
- Row 2: 2*2 - 1 = 3 stars
- Row 3: 2*3 - 1 = 5 stars
- This creates the pyramid shape
9. Understanding the Patterns
Right Half Pyramid:
- Simplest pattern
- Stars increase linearly: row i has i stars
- No spaces needed
Left Half Pyramid:
- Requires spaces for alignment
- Spaces decrease as stars increase
- Creates right-aligned appearance
Full Pyramid:
- Most complex of the three
- Requires both spaces (centering) and odd number of stars
- Creates symmetric triangle shape
10. Other Patterns (Mentioned but not shown in code)
The program mentions 15 different patterns including:
- Inverted Pyramid: reverse of full pyramid
- Diamond: combination of full pyramid and inverted pyramid
- Hollow Pyramid: stars only on edges
- Hourglass: inverted pyramid + pyramid
- Arrow: combination patterns
- Cross, Plus: geometric shapes
- Square, Rectangle: grid patterns
11. When to Use Pattern Printing
Educational Purposes:
- Learning nested loops
- Understanding loop control
- Developing logical thinking
Interview Preparation:
- Common coding interview questions
- Tests pattern recognition skills
- Demonstrates loop mastery
Visual Programming:
- Creating ASCII art
- Text-based graphics
- Console-based displays
12. Important Considerations
Loop Control:
- Outer loop typically goes from 1 to rows
- Inner loops control what's printed in each row
- Careful with loop boundaries (<= vs <)
Spacing:
- Single space " " vs double space " " matters
- Affects alignment and appearance
- Test with different row counts
Formula Understanding:
- 2*i - 1 for odd numbers
- rows - i for decreasing spaces
- Understanding these formulas is key
13. return 0;
This ends the program successfully.
Summary
- Star patterns use nested loops: outer for rows, inner for columns.
- Right Half Pyramid: simplest, stars increase linearly per row.
- Left Half Pyramid: requires spaces for right alignment.
- Full Pyramid: requires spaces for centering and odd number of stars (2*i - 1).
- Pattern printing develops loop control and logical thinking skills.
- Understanding formulas (2*i - 1, rows - i) is essential for complex patterns.
- Multiple pattern variations exist: inverted, hollow, diamond, etc.
This program is fundamental for beginners learning nested loops, understanding pattern recognition, and preparing for more complex pattern problems in C++ programs.