-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrintLikeWave.java
67 lines (59 loc) · 1.25 KB
/
PrintLikeWave.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*Print Like a Wave
For two-dimensional integer array/list of size (N x M), print the array/list in a sine wave order, i.e, print the first column top to bottom, next column bottom to top and so on.*/
import java.util.Scanner;
public class PrintLikeWave
{
public static void LikeWave(int arr[][])
{
if (arr.length == 0) {
return;
}
int i, j;
if (arr.length == 0)
{
return;
}
int rows=arr.length;
int cols=arr[0].length;
for(j=0;j<cols;j++)
{
if(j%2==0)
{
for(i=0;i<rows;i++)
{
System.out.print(arr[i][j]+ " ");
}
}
else
{
for(i=rows-1;i>=0;i--)
{
System.out.print(arr[i][j]+ " ");
}
}
}
}
public static int[][] takeInput()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the row size");
int rows=sc.nextInt();
System.out.println("Enter the column size");
int cols=sc.nextInt();
int[][] arr=new int[rows][cols];
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
System.out.println("Enter the element at "+ i+ "th row and "+j+"th column");
arr[i][j]=sc.nextInt();
}
}
return arr;
}
public static void main(String args[])
{
int [][]input=takeInput();
LikeWave(input);
}
}