0% found this document useful (0 votes)
180 views1 page

Print 2D Array in Snake Pattern

This Java code defines a snakeprint method that prints out the elements of a 2D array in a "snake" pattern (first row left to right, second row right to left, etc). It takes user input to create a 2D integer array, prints it normally, then calls snakeprint to print it in the snake pattern across rows.

Uploaded by

abc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
180 views1 page

Print 2D Array in Snake Pattern

This Java code defines a snakeprint method that prints out the elements of a 2D array in a "snake" pattern (first row left to right, second row right to left, etc). It takes user input to create a 2D integer array, prints it normally, then calls snakeprint to print it in the snake pattern across rows.

Uploaded by

abc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import [Link].

Scanner;
public class testcode{
public static void snakeprint(int ar[][]){
[Link]("Elments printed in the order of snake is : ");
[Link](" ");

for(int i = 0 ; i < [Link] ; i++){


if( i % 2 == 0){
for(int j = 0 ; j < ar[0].length ; j++){
[Link](ar[i][j] + " ");
}
}else if( i % 2 != 0){
for(int j = ar[0].length - 1 ; j >=0 ; j--){
[Link](ar[i][j] + " ");
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("enter the number of rows");
int r = [Link]();
[Link]("enter the number of columns");
int c = [Link]();
int ar[][] = new int[r][c];

for(int i = 0 ; i < [Link] ; i++){


for(int j = 0 ; j <ar[0].length ; j++){
[Link]("enter the element ");
ar[i][j] = [Link]();
}
}

[Link](" ");
for(int i = 0 ; i < [Link] ; i++){
for(int j = 0 ; j < ar[0].length ; j++){
[Link](ar[i][j] + " ");
}
[Link](" ");
}
[Link](" ");

snakeprint(ar);

}
}

You might also like