Pattern Programming Rules Full
Pattern Programming Rules Full
Output:
for first 0
a) Print a row
We can use a the i - (row) variable with a specified indexed value.
c) Print a diagonal
NOTE: for anything which is not a straight line, we have to derive a condition which satisfies all the blocks
( 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
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
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
}
}
}
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();
}
}
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();
}
2. For printing triangles we will either give less than (<) or greater than( > ) operators for the diagona
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
For printing these we have to merge two complete triangles with the help of && operator
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++) {
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
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();
}
}
}
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();
}
}
}