0% found this document useful (0 votes)
101 views

Snake Array Print 2d

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)
101 views

Snake Array Print 2d

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
You are on page 1/ 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