-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPattern.java
More file actions
140 lines (130 loc) · 3.2 KB
/
Pattern.java
File metadata and controls
140 lines (130 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
public class Pattern{
public static void main(String args[]){
int n = 4;
int m = 4;
// 1. Solid Rectangle:
// for(int i = 1; i <= n; i++) {
// for(int j = 1; j <= m; j++) {
// System.out.print("*");
// }
// // System.out.print("\n");
// //OR
// System.out.println();
// }
// 2. Hollow Rectangle:
// Rows => Outer Loops(i) |
// column => Inner Loops(j) ---
// for(int i=1; i<=n; i++) {
// for(int j=1; j<=m; j++) {
// // if j==m or j==1 are wrong then after the condition of i==1 or i==n is also true
// if(i==1 || i==n || j==1 || j==m) {
// System.out.print("*");
// }
// else {
// System.out.print(" ");
// }
// }
// System.out.println();
// }
// 3. Half Pyramid:
// if the rows value is 2 the its column value is 2
// for(int i=1; i<=n; i++) {
// for(int j=1; j<=i; j++) {
// System.out.print("*");
// // OR
// // System.out.print(""+j);
// }
// System.out.println();
// }
// 4. Inverted Half Pyramid:
// Giving same Output:
// for(int i=1; i<=n; i++) { // OR for(int i=n; i>=1; i--)
// for(int j=n; j>=i; j--) { // OR for(int j=1; j<=i; j++)
// System.out.print("*");
// }
// System.out.println();
// }
// 5. Inverted Half Pyramid(rotated by 180 deg):
// for(int i=1; i<=n; i++) {
// for(int j=1; j<=n-i; j++) {
// System.out.print(" ");
// }
// for(int j=1; j<=i; j++) {
// System.out.print("*");
// }
// System.out.println();
// }
// OR
// for(int i=n; i>=1; i--) {
// for(int j=1; j<i; j++) {
// System.out.print(" ");
// }
// for(int j=0; j<=n-i; j++) {
// System.out.print("*");
// }
// System.out.println();
// }
// 6. Half Pyramid with Numbers:
// for(int i=1; i<=n; i++) {
// for( int j=1; j<=i; j++) {
// System.out.print(+j);
// }
// System.out.println();
// }
// 7. Inverted Half Pyramid With Numbers:
// for(int i=n; i>=1; i--) {
// for(int j=1; j<=i; j++) {
// System.out.print(+j);
// }
// System.out.println();
// }
//OR
// for(int i=1; i<=n; i++) {
// for(int j=1; j<=n-i+1; j++) {
// System.out.print(+j);
// }
// System.out.println();
// }
// 8. Floyd's Triangle with Numbers:
// int n = 5;
// int number = 1;
// for (int i=1; i<=n; i++) { //i=5
// for (int j=1; j<=i; j++) { //j=5
// System.out.print(number); //number=16
// System.out.print(" ");
// number++;
// }
// System.out.println();
// }
// 9. 0-1 Triangle with 0 & 1 Numbers:
// int n = 5;
// int number = 1;
// for (int i=1; i<=n; i++) {
// for (int j=1; j<=i; j++) {
// System.out.print(number);
// System.out.print(" ");
// if (number == 1) {
// number--;
// }
// else{
// number++;
// }
// }
// System.out.println();
// }
// OR
for (int i=1; i<=n; i++) {
for (int j=1; j<=i; j++) {
int sum = i+j;
if((sum%2) == 0){
System.out.print(1);
}
else{
System.out.print(0);
}
System.out.print(" ");
}
System.out.println();
}
}
}