Patterns
PATTERNS
Agenda
Introduction to Pattern Printing
Types of Patterns
Star Patterns
Number Patterns
Advanced Patterns
Pyramid Patterns
PATTERNS
Introduction to Pattern Printing
Pattern printing involves creating visual outputs using loops.
Used in algorithm design and coding interviews.
Develop logical thinking and coding skills.
Basics of Pattern Printing
Loops: Nested loops (outer for rows, inner for columns).
Spaces: Manage alignment in patterns.
Symbols: Decide what to print (e.g., *, numbers, alphabets).
PATTERNS
Types of Patterns
Star Patterns
Number Patterns
Pyramid Patterns
Character Patterns
Miscellaneous Patterns
PATTERNS
Star Patterns
Simple and most common type.
for (int row = 1; row <= 5; row++)
{
for (int col = 1; col <= row; col++)
{
System.out.print("* ");
}
System.out.println();
}
*
**
***
****
*****
PATTERNS
Star Patterns – Code
for (int row = 1; row <= 5; row++)
{
for (int col = 1; col <= row; col++)
{
System.out.print("* ");
}
System.out.println();
}
*****
****
***
**
*
PATTERNS
Reverse Star Pattern – Code
for (int row = 5; row >= 1; row--)
{
for (int col = 1; col <= row; col++)
{
System.out.print("* ");
}
System.out.println();
}
*
**
***
****
*****
PATTERNS
Right-Aligned Star Pattern
for (int row = 1; row <= 5; row++)
{
for (int col = 1; col <= 5 - row; col++)
{
System.out.print(" ");
}
for (int col = 1; col <= row; col++)
{
System.out.print("* ");
}
System.out.println();
}
*
**
***
****
*****
PATTERNS
Right-Aligned Star Pattern (Alternate approach)
for (int row = 1; row <= 5; row++)
{
for (int col = 1; col <= 5; col++)
{
if(row+col > 5)
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
*
**
***
****
*****
PATTERNS
Number Patterns
•Patterns using numbers.
for (int row = 1; row <= 5; row++)
{
for (int col = 1; col <= row; col++)
{
System.out.print(col + " ");
}
System.out.println();
}
1
12
123
1234
12345
PATTERNS
Reverse Number Pattern
for (int row = 5; row >= 1; row--)
{
for (int col = 1; col <= row; col++)
{
System.out.print(col + " ");
}
System.out.println();
}
12345
1234
123
12
1
PATTERNS
Pyramid Patterns
for (int row = 1; row <= 5; row++)
{
for (int col = 1; col <= 5 - row; col++)
{
System.out.print(" ");
}
for (int col = 1; col <= row; col++)
{
System.out.print("* ");
}
System.out.println();
}
*
**
***
****
*****
PATTERNS
Inverted Pyramid
for (int row = 5; row >= 1; row--)
{
for (int col = 1; col <= 5 - row; col++)
{
System.out.print(" ");
}
for (int col = 1; col <= row; col++)
{
System.out.print("* ");
}
System.out.println();
}
*****
****
***
**
*
PATTERNS
Diamond Pattern
for (int row = 1; row <= 5; row++)
{
for (int col = 1; col <= 5 - row; col++)
System.out.print(" ");
for (int col = 1; col <= row; col++)
System.out.print("* ");
System.out.println();
}
for (int row = 4; row >= 1; row--)
{
for (int col = 1; col <= 5 - row; col++)
System.out.print(" ");
for (int col = 1; col <= row; col++)
System.out.print("* ");
System.out.println();
}
PATTERNS
Diamond Pattern (Alternative approach)
int line = 1;
for (int row = 1; row <= 5*2-1; row++)
{
for (int col = 1; col <= 5 - line; col++)
{
System.out.print(" ");
}
for (int col = 1; col <= line; col++)
{
System.out.print("* ");
}
line += (row<5) ? 1 : -1 ;
System.out.print("\n");
}
PATTERNS
Output:
*
**
***
****
*****
****
***
**
*
PATTERNS
Character Patterns
for (int row = 1; row <= 5; row++)
{
for (char ch = 'A'; ch < 'A' + row; ch++)
{
System.out.print(ch + " ");
}
System.out.println();
}
A
AB
ABC
ABCD
ABCDE
PATTERNS
Advanced Pattern - Hollow Rectangle
for (int row = 1; row <= 5; row++)
{
for (int col = 1; col <= 5; col++)
{
if (row == 1 || row == 5 || col == 1 || col == 5)
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
* * * * *
* *
* *
* *
* * * * *
PATTERNS
Applications of Pattern Printing
Logical Thinking
Coding Interviews
Algorithm Design
Best Practices
Use nested loops effectively.
Optimize for readability.
Start with simple patterns and build complexity.
PATTERNS
prectice questions
1) 4) 7) 10)
***** ********* ***** *
***** ******* * * *
***** ***** * * *****
***** *** * * *
* ***** *
*****
2) 8)
5) 6)
* * *
***** *
** ***** * * **
*** ***** * ***
**** ***** * * ****
***** ***** * * *****
3) ****
* 9) ***
***** **
***
** ** *
***** * * *
******* ** **
********* *****
PATTERNS
11) 14) 17) 20)
* * 11111 11011 1
** ** 00000 11011 12
11111 00000 123
*****
00000 11011 1234
** ** 11011
11111 12345
* *
12) 15) 18)
***** 11111 11111
** ** 11111 11011
* * 11011 10101
** ** 11111 11011
***** 11111 11111
13)
16) 19)
10101 11111
11111
01010 2222
10001
10101 10001 333
01010 10001 44
10101 11111 5
PATTERNS
21) 24) 27) 30)
12345 1 3 5 7 12345 1 1
2 4 6 8 23455 12 21
23456
9 11 13 15 34555 123 321
34567 10 12 14 16 45555 1234 4321
45678 55555 1234554321
56789 25)
22) 11112 28)
12345 32222 55555
23451 33334 54444
34512 54444 54333
45123 54322
26) 54321
51234
1
23) 21 29)
1 123 1
23 4321 121
135 12345 12321
2468 1234321
13579 123454321
THANK YOU