Pattern Programming
A pattern is basically characters and numbers being written in N * N matrix
Output:
New Section 8 Page 1
For printing different patterns in the matrix we have to write the condition
For writing condition the fixed formula for indexes are
for first 0
For mid n/2
For last n-1
1. To print the Straight lines:
a) Print a row
We can use a the i - (row) variable with a specified indexed value.
New Section 8 Page 2
b) Print a column
We can use the j - ( column variable) with a specified index
c) Print a diagonal
NOTE: for anything which is not a straight line, we have to derive a condition which satisfies all the blocks
-> for backward facing diagonal
( i , j)
0 0
1 1
2 2 ==> i == j
3 3
4 4
New Section 8 Page 3
2 2 ==> i == j
3 3
4 4
If the backward facing diagonal is moving ahead--> then the condition we can use is i==j-1
If the backward facing diagonal is moving down --> then the condition we can use is i==j+1
-> for forward facing diagonal
(i,j)
0 4
1 3
2 2 ==> i + j == n-1
3 1
4 0
If the forward facing diagonal is moving behind--> then the condition we can use is i+j==(n-1) -N
If the forward facing diagonal is moving down --> then the condition we can use is i+j==(n-1)+N
New Section 8 Page 4
2. To print multiple lines
Here, every line will have its own condition so to print multiple line condition will be merges using or ||
3. To print less than one line
Here, we will start writing in one straight line but we will stop before some block( incomplete), so we will write
another condition to limit the line.
• If line if a row then other condition is for j
If line is a column then the other condition is for I
New Section 8 Page 5
• If line is a column then the other condition is for I
public class P1 {
// & Skeleton Program
public static void main(String[] args) {
int n = 5; // defines the size of the matrix
/*
* ! Make sure the size is in odd number ! This creates a perfectly defined !
* mid-element /mid-index
*/
// Nested loops [ iterate over the blocks ]
for (int i = 0; i < n; i++)// rows
{
for (int j = 0; j < n; j++)// columns
{
if (i == n / 2 || j == n / 2 && i <= n / 2 || i + j == n - 1 && i >= n / 2
|| i == j && i >= n / 2)
System.out.print(" *");
// ? print will move the cursor to next column
else
System.out.print(" ");
// ? this print statement is there to fill up an empty block
}
System.out.println();
// ? println will send the cursor for the next row
}
}
}
New Section 8 Page 6
Triangle and shape patterns
1. The matrix can be divided into 4 parts
a) Upper half
public class P2 {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i <= n / 2)
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
}
}
b) Lower half
public class P2 {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i >= n / 2)
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
}
New Section 8 Page 7
}
}
c) Forward half
public class P2 {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j >= n / 2)
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
}
}
d) Backward half
public class P2 {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j <= n / 2)
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
New Section 8 Page 8
}
}
}
2. For printing triangles we will either give less than (<) or greater than( > ) operators for the diagona
i. Backward facing upper triangle
public class P2 {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i <= j)// backward facing diagonal
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
}
}
ii. Backward facing lower triangle
public class P2 {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i >= j)// backward facing diagonal
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
}
}
New Section 8 Page 9
}
iii. Forward facing upper triangle
public class P2 {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i + j <= n - 1)// forward facing diagonal
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
}
}
iv. Forward facing lower triangle
public class P2 {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i + j >= n - 1)// forward facing diagonal
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
}
}
3) For printing the semi triangles
New Section 8 Page 10
3) For printing the semi triangles
For printing these we have to merge two complete triangles with the help of && operator
Backward facing Forward facing
triangle triangle
i<=j && i+j<=n-1
i>=j && i+j<=n-1
i<=j && i+j>=n-1
i>=j && i+j>=n-1
4) For printing number triangles
They are always written in
public class P2 {
public static void main(String[] args) {
int n = 5;
int k = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
New Section 8 Page 11
for (int j = 0; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Here we are printing a sequence of numbers in a triangle pattern
public class P2 {
public static void main(String[] args) {
int n = 5;
int k = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(++k + "\t");
}
System.out.println();
}
}
}
1
0 1
0 1 0
1 0 1 0
1 0 1 0 1
public class P2 {
public static void main(String[] args) {
int n = 5;
int k = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
New Section 8 Page 12
for (int j = 0; j <= i; j++) {
System.out.print(++k % 2 + "\t");
}
System.out.println();
}
}
}
Index values
0
11
222
3333
44444
Common rows
public class P2 {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(i + "\t");
}
System.out.println();
}
}
}
0
01
012
0123
01234
Common columns
public class P2 {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(j + "\t");
}
System.out.println();
}
}
}
New Section 8 Page 13
1
12
123
1234
12345
public class P2 {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(j + 1 + "\t");
}
System.out.println();
}
}
}
4
34
234
1234
01234
public class P2 {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
System.out.print((n - 1) - (i - j) + "\t");
}
System.out.println();
}
}
}
.
1
22 2
333 33
4444 444
55555 5555
public class P2 {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i + j >= n - 1)
System.out.print(i + 1 + " ");
else
System.out.print(" ");
}
New Section 8 Page 14
}
for (int j = 0; j < n; j++) {
if (i > j)
System.out.print(i + 1 + " ");
else
System.out.print(" ");
}
System.out.println();
}
}
}
New Section 8 Page 15