-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseEachRow.java
More file actions
39 lines (33 loc) · 1 KB
/
ReverseEachRow.java
File metadata and controls
39 lines (33 loc) · 1 KB
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
package ArraysD2;
import java.util.Scanner;
public class ReverseEachRow {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
System.out.println("Enter the dimensions of the 2D array: ");
int n = scn.nextInt();
int m = scn.nextInt();
int[][] mat = new int[n][m];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
mat[i][j] = scn.nextInt();
}
}
for (int i = 0; i < n; i++) {
int a = 0, b = m - 1;
while (a < b) {
int temp = mat[i][a];
mat[i][a] = mat[i][b];
mat[i][b] = temp;
a++;
b--;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(mat[i][j] + " ");
}
System.out.println();
}
}
}