Print Matrix in snake Pattern Java
PROGRAM TO PRINT MATRIX IN SNAKE PATTERN
OUTPUT
10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32
import
java.util.*;
class
MAIN{
static
void
print(
int
[][] mat)
{
// Traverse through all rows
for
(
int
i =
0
; i < mat.length; i++)
{
// If current row is even, print from
// left to right
if
(i %
2
==
0
)
{
for
(
int
j =
0
; j < mat[
0
].length; j++)
System.out.print(mat[i][j] +
" "
);
// If current row is odd, print from
// right to left
}
else
{
for
(
int
j = mat[
0
].length -
1
; j >=
0
; j--)
System.out.print(mat[i][j] +
" "
);
}
}
}
// Driver code
public
static
void
main(String[] args)
{
int
mat[][] =
new
int
[][]
{
{
10
,
20
,
30
,
40
},
{
15
,
25
,
35
,
45
},
{
27
,
29
,
37
,
48
},
{
32
,
33
,
39
,
50
}
};
print(mat);
}
}
10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32
Comments
Post a Comment