-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakePattern.java
More file actions
31 lines (29 loc) · 780 Bytes
/
SnakePattern.java
File metadata and controls
31 lines (29 loc) · 780 Bytes
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
package Matrix;
/**
* @author Vishal Singh */
public class SnakePattern {
static void printSnake(int[][] arr){
for (int i = 0; i < arr.length; i++) {
if (i%2 == 0){
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j]+" ");
}
}
else {
for (int j = arr[i].length-1; j >= 0; j--) {
System.out.print(arr[i][j]+" ");
}
}
System.out.println("");
}
}
public static void main(String[] args) {
int[][] arr = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16},
};
printSnake(arr);
}
}