You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lessons/print-patterns.md
+193Lines changed: 193 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -5,3 +5,196 @@ order: "4C"
5
5
section: "Arrays"
6
6
description: "learn Arrays"
7
7
---
8
+
9
+
Java pattern program enhances the coding skill, logic, and looping concepts. It is mostly asked in Java interview to check the logic and thinking of the programmer. We can print a Java pattern program in different designs. To learn the pattern program, we must have a deep knowledge of the Java loop, such as for loop do-while loop. In this section, we will learn how to print a pattern in Java.
10
+
11
+
## **We have classified the Java pattern program into three categories:**
12
+
- Star Pattern
13
+
- Number Pattern
14
+
- Character Pattern
15
+
16
+
### **Here we explain each category with examples :**
17
+
18
+
## **Star Pattern :**
19
+
20
+
_**Qn1 : Right Triangle Star Pattern**_
21
+
22
+
### **Code :**
23
+
```java
24
+
publicclassRightTriangleStarPattern
25
+
{
26
+
publicstaticvoidmain(Stringargs[])
27
+
{
28
+
//i for rows and j for columns
29
+
//row denotes the number of rows you want to print
30
+
int i, j, row =4;
31
+
//outer loop for rows
32
+
for(i=0; i<row; i++)
33
+
{
34
+
//inner loop for columns
35
+
for(j=0; j<=i; j++)
36
+
{
37
+
//prints stars
38
+
System.out.print("* ");
39
+
}
40
+
//throws the cursor in a new line after printing each line
41
+
System.out.println();
42
+
}
43
+
}
44
+
}
45
+
```
46
+
### **Output :**
47
+
```
48
+
*
49
+
* *
50
+
* * *
51
+
* * * *
52
+
```
53
+
54
+
### **Explanation & Approch of Solution :**
55
+
Whenever you design logic for a pattern program, first draw that pattern in the blocks, as we have shown in the following image. The figure presents a clear look of the pattern.
56
+
57
+
58
+

59
+
60
+
In the above pattern, the row is denoted by i and the column is denoted by j. We see that the first row prints only a star. The second-row prints two stars, and so on. The colored blocks print the spaces.
61
+
62
+
### **Iteration-1 :**
63
+
```
64
+
For i=0, 0<4 (true)
65
+
For j=0, j<=0 (true)
66
+
```
67
+
The first print statement prints a star at the first row and the second println statement prints the spaces and throws the cursor at the next line.
68
+
69
+
**Output Console :**
70
+
```
71
+
*
72
+
```
73
+
74
+
### **Iteration-2 :**
75
+
```
76
+
For i=1, 1<4 (true)
77
+
For j=1, 1<=1 (true)
78
+
```
79
+
The first print statement prints two stars at the second row and the second println statement prints the spaces and throws the cursor at the next line.
80
+
81
+
**Output Console :**
82
+
```
83
+
*
84
+
* *
85
+
```
86
+
87
+
### **Iteration-3 :**
88
+
```
89
+
For i=2, 2<4 (true)
90
+
For j=2, 2<=2 (true)
91
+
```
92
+
The first print statement prints three stars at the third row and the second println statement prints the spaces and throws the cursor at the next line.
93
+
94
+
**Output Console :**
95
+
```
96
+
*
97
+
* *
98
+
* * *
99
+
```
100
+
101
+
### **Iteration-4 :**
102
+
```
103
+
For i=3, 3<4 (true)
104
+
For j=3, 3<=3 (true)
105
+
```
106
+
The first print statement prints four stars at the fourth row and the second println statement prints the spaces and throws the cursor at the next line.
107
+
108
+
**Output Console :**
109
+
```
110
+
*
111
+
* *
112
+
* * *
113
+
* * * *
114
+
```
115
+
**Now the value of i and j is increased to 4.**
116
+
117
+
```
118
+
For i=4, 4<4 (false)
119
+
```
120
+
The execution of the program will terminate when the value of i will be equal to the number of rows.
121
+
122
+
123
+
## **Number Pattern :**
124
+
125
+
_**Qn1 : Right Triangle Number Pattern**_
126
+
127
+
### **Code :**
128
+
```java
129
+
publicclassRightTriangleNumberPattern
130
+
{
131
+
publicstaticvoidmain(String[] args)
132
+
{
133
+
//i for rows and j for columns
134
+
//row denotes the number of rows you want to print
135
+
int i, j, row =4;
136
+
//outer loop for rows
137
+
for (i =1; i <= row; i++)
138
+
{
139
+
//inner loop for columns
140
+
for (j =1; j <= i; j++)
141
+
{
142
+
//print the current value of j
143
+
System.out.print(j +"");
144
+
}
145
+
//throws the cursor in a new line after printing each line
146
+
System.out.println();
147
+
}
148
+
}
149
+
}
150
+
```
151
+
### **Output :**
152
+
```
153
+
1
154
+
1 2
155
+
1 2 3
156
+
1 2 3 4
157
+
```
158
+
159
+
160
+
## **Character Pattern :**
161
+
162
+
_**Qn1 : Right Triangle Character Pattern**_
163
+
164
+
### **Code :**
165
+
```java
166
+
publicclassRightTriangleCharacterPattern
167
+
{
168
+
publicstaticvoidmain(String[] args)
169
+
{
170
+
//i for rows and j for columns
171
+
//row denotes the number of rows you want to print
172
+
int i, j, row =4;
173
+
//declear alphabet variable to print the value
174
+
int alphabet =65;
175
+
//outer loop for rows
176
+
for (i =1; i <= row; i++)
177
+
{
178
+
//inner loop for columns
179
+
for (j =1; j <= i; j++)
180
+
{
181
+
//print the current value of alphabet in character form
182
+
System.out.print((char) alphabet +"");
183
+
}
184
+
//increment the alphabet variable by 1
185
+
alphabet++;
186
+
//throws the cursor in a new line after printing each line
0 commit comments