#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
// Right Triangle
cout << "\nRight Triangle:" << endl;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
// Inverted Right Triangle
cout << "\nInverted Right Triangle:" << endl;
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
cout << "* ";
}
cout << endl;
}
// Hollow Triangle
cout << "\nHollow Triangle:" << endl;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1 || j == i || i == rows) {
cout << "* ";
} else {
cout << " ";
}
}
cout << endl;
}
return 0;
}Output
Enter number of rows: 5 Right Triangle: * * * * * * * * * * * * * * * Inverted Right Triangle: * * * * * * * * * * * * * * * Hollow Triangle: * * * * * * * * * * * *
This program teaches you how to print various triangle patterns in C++ using nested loops. Triangle patterns are fundamental exercises that help understand loop control, conditional printing, and pattern formation. They are essential for developing logical thinking and are frequently asked in programming interviews.
1. What This Program Does
The program prints different triangle patterns based on the number of rows entered by the user. For example, with 5 rows, it creates:
- Right Triangle: increasing stars from left
- Inverted Right Triangle: decreasing stars from top
- Hollow Triangle: stars only on edges
Triangle patterns demonstrate how to control the printing of characters to form geometric shapes.
2. Header File Used
#include <iostream>
This header provides:
- cout for displaying output
- cin for taking input from the user
3. Understanding Triangle Patterns
Key Concepts:
- Triangle patterns form triangular shapes using characters (usually stars)
- Outer loop controls rows (vertical)
- Inner loops control what's printed in each row (horizontal)
- Different patterns require different loop logic
Pattern Variations:
- Right Triangle: stars increase per row
- Inverted Triangle: stars decrease per row
- Hollow Triangle: stars only on perimeter
- Mirrored Triangle: right-aligned pattern
4. Declaring Variables
The program declares: int rows;
- rows stores the number of rows entered by the user.
- This determines the size of all triangle 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 Triangle
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 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: Inverted Right Triangle
for (int i = rows; i >= 1; i--) { for (int j = 1; j <= i; j++) { cout << "* "; } cout << endl; }
How it works:
- Outer loop (i): iterates from rows down to 1 (decreasing)
- Inner loop (j): prints i stars in row i
- Row 1: 5 stars, Row 2: 4 stars, Row 3: 3 stars, etc.
Output (for rows = 5):
Alternative Approach:
Can also use: for (int i = 1; i <= rows; i++) with inner loop: for (int j = 1; j <= rows - i + 1; j++)
8. Pattern 3: Hollow Triangle
for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { if (j == 1 || j == i || i == rows) { cout << "* "; } else { cout << " "; } } cout << endl; }
How it works:
- Outer loop (i): iterates through rows
- Inner loop (j): prints stars only at edges
- Condition: print star if first column (j == 1), last column (j == i), or last row (i == rows)
- Otherwise print spaces
Output (for rows = 5):
-
-
-
*
9. Understanding the Patterns
Right Triangle:
- Simplest pattern
- Stars increase linearly: row i has i stars
- No conditional logic needed
Inverted Right Triangle:
- Reverse of right triangle
- Stars decrease: row i has (rows - i + 1) stars
- Can use decreasing loop or calculate stars
Hollow Triangle:
- More complex - requires conditional printing
- Only print stars at boundaries (edges)
- Uses if-else to decide star vs space
10. Other Patterns (Mentioned but not shown in code)
The program mentions 10 different patterns including:
- Mirrored Triangle: right-aligned with spaces
- Number Triangle: numbers instead of stars
- Alphabet Triangle: letters instead of stars
- Pascal's Triangle: mathematical pattern
- Floyd's Triangle: sequential numbers
- Various combinations and variations
11. When to Use Triangle Patterns
Educational Purposes:
- Learning nested loops
- Understanding conditional printing
- Developing pattern recognition
Interview Preparation:
- Common coding interview questions
- Tests logical thinking
- Demonstrates loop control mastery
Visual Programming:
- Creating geometric shapes
- ASCII art generation
- Console graphics
12. Important Considerations
Loop Direction:
- Increasing: for (int i = 1; i <= rows; i++)
- Decreasing: for (int i = rows; i >= 1; i--)
- Choose based on pattern requirements
Conditional Printing:
- Hollow patterns require if-else logic
- Check boundaries: first/last row, first/last column
- Balance between stars and spaces
Spacing:
- Single space vs double space affects appearance
- Consider spacing when printing characters
- Test with different row counts
13. return 0;
This ends the program successfully.
Summary
- Triangle patterns use nested loops with outer loop for rows, inner for columns.
- Right Triangle: simplest, stars increase linearly (i stars in row i).
- Inverted Triangle: stars decrease (rows - i + 1 stars in row i).
- Hollow Triangle: requires conditional logic to print only edges.
- Understanding loop direction and conditional printing is essential.
- Multiple variations exist: mirrored, number, alphabet triangles.
- Pattern printing develops logical thinking and loop control skills.
This program is fundamental for beginners learning nested loops, understanding conditional printing, and preparing for more complex pattern problems in C++ programs.