π Printing Patterns
One-line summary: Master nested loops and logical thinking through visual pattern printing β the foundation for understanding 2D array manipulation and algorithmic problem-solving.
Conceptβ
Pattern printing problems help develop:
- Nested loop control β understanding row-column relationships
- Mathematical thinking β finding formulas for positions and values
- Logical reasoning β breaking complex patterns into simple rules
- 2D visualization β essential for matrix and grid problems
Common patterns: stars, numbers, alphabets, geometric shapes, mathematical sequences.
Common Techniquesβ
Basic Triangle Patternβ
function printTriangle(n) {
for (let i = 1; i <= n; i++) {
let row = '';
for (let j = 1; j <= i; j++) {
row += '* ';
}
console.log(row);
}
}
Number Pyramidβ
function numberPyramid(n) {
for (let i = 1; i <= n; i++) {
let spaces = ' '.repeat(n - i);
let numbers = '';
for (let j = 1; j <= i; j++) {
numbers += j + ' ';
}
console.log(spaces + numbers);
}
}
Diamond Patternβ
function diamond(n) {
// Upper half
for (let i = 1; i <= n; i++) {
console.log(' '.repeat(n - i) + '*'.repeat(2 * i - 1));
}
// Lower half
for (let i = n - 1; i >= 1; i--) {
console.log(' '.repeat(n - i) + '*'.repeat(2 * i - 1));
}
}
Practice Problemsβ
Basic Patterns (Easy)β
| Problem | Difficulty | Solution |
|---|
| Concentric Rectangle | Easy | View Solution | | Print Floyd's Triangle | Easy | View Solution | | Print Matrix Diagonal | Easy | View Solution | | Print Rectangle Pattern | Easy | View Solution | | Print X in Box | Easy | View Solution | | Star Triangle Pattern | Easy | | | Right Triangle Pattern | Easy | | | Inverted Triangle | Easy | | | Hollow Rectangle | Easy | | | Square Pattern | Easy | | | Number Triangle | Easy | | | Alphabet Triangle | Easy | | | Plus Pattern | Easy | | | Cross Pattern | Easy | | | Simple Diamond | Easy | | | Arrow Pattern | Easy | | | Checkerboard Pattern | Easy | | | Binary Pattern | Easy | | | Multiplication Table Pattern | Easy | | | Even-Odd Pattern | Easy | |
Intermediate Patterns (Medium)β
| Problem | Difficulty | Solution |
|---|---|---|
| Number Pyramid | Medium | |
| Diamond Pattern | Medium | |
| Pascal's Triangle | Medium | |
| Butterfly Pattern | Medium | |
| Zigzag Pattern | Medium | |
| Spiral Matrix | Medium | |
| Heart Pattern | Medium | |
| Number Diamond | Medium | |
| Alphabet Pattern | Medium | |
| Hourglass Pattern | Medium | |
| Rhombus Pattern | Medium | |
| Wave Pattern | Medium | |
| Hollow Diamond | Medium | |
| Fibonacci Triangle | Medium | |
| Prime Number Pattern | Medium | |
| Matrix Spiral | Medium | |
| Sandglass Pattern | Medium | |
| Hexagon Pattern | Medium | |
| Star Diamond | Medium | |
| Number Spiral | Medium |
Advanced Patterns (Hard)β
| Problem | Difficulty | Solution |
|---|---|---|
| Complex Fractal Pattern | Hard | |
| Sierpinski Triangle | Hard | |
| Matrix Rotation Pattern | Hard | |
| Multi-layer Diamond | Hard | |
| 3D Cube Pattern | Hard | |
| Mandala Pattern | Hard | |
| Celtic Knot Pattern | Hard | |
| Maze Pattern | Hard | |
| Tessellation Pattern | Hard | |
| Golden Ratio Spiral | Hard |
CodeChef Pattern Problemsβ
| Problem | Difficulty | Solution |
|---|---|---|
| CC β Pattern Printing (PATTERN) | Easy | |
| CC β Star Pattern (STARPTR) | Easy | |
| CC β Number Pattern (NUMPTR) | Medium | |
| CC β Triangle Pattern (TRIPTR) | Medium | |
| CC β Diamond Design (DIAMPTR) | Medium | |
| CC β Complex Pattern (COMPLXPTR) | Hard |
Tips for Pattern Problemsβ
- Analyze the pattern first β identify rows, columns, and relationships
- Find the mathematical formula β express position in terms of row/column indices
- Handle spaces carefully β leading spaces often determine alignment
- Test with small inputs β verify logic with n=3 or n=4 before scaling
- Break complex patterns β divide into simpler sub-patterns
Related Topicsβ
- Matrix β 2D array manipulation
- Math β mathematical sequences and formulas
- Recursion β recursive pattern generation
- School Basics β fundamental programming concepts
β Back to Home Β· Β© sparshjaswal