You are on page 1of 1

import java.util.

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

for(int i = 0 ; i < ar.length ; i++){


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

for(int i = 0 ; i < ar.length ; i++){


for(int j = 0 ; j <ar[0].length ; j++){
System.out.println("enter the element ");
ar[i][j] = sc.nextInt();
}
}

System.out.println(" ");
for(int i = 0 ; i < ar.length ; i++){
for(int j = 0 ; j < ar[0].length ; j++){
System.out.print(ar[i][j] + " ");
}
System.out.println(" ");
}
System.out.println(" ");

snakeprint(ar);

}
}

You might also like