print matrix elements in wave form in java.

 import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

      Scanner sc = new Scanner(System.in);

      System.out.println("enter row and column size=");

      int m=sc.nextInt();

      int n=sc.nextInt();

      int[][] arr=new int[m][n];

      System.out.println("enter elements=");

      for(int i=0; i<m; i++){

            for(int j=0; j<n; j++){

                  arr[i][j]=sc.nextInt();

            }

      }

      // logic

      for(int i=0; i<m; i++){

             if(i%2==0){

                    for(int j=0; j<n; j++){

                    System.out.print(arr[i][j]+" ");

                    }

             }

            else{

                 for(int j=n-1; j>=0; j--){ 

                   System.out.print(arr[i][j]+" ");

                 }

      } 

}

}

}

output->

enter row and column size=

3

3

enter elements=

12

45

78

98

53

61

25

58

64

12 45 78 61 53 98 25 58 64 

Comments

Popular posts from this blog

Introduction of java Programming language.

Stack data structure.