-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotatematrix90deg.java
More file actions
34 lines (34 loc) · 1010 Bytes
/
rotatematrix90deg.java
File metadata and controls
34 lines (34 loc) · 1010 Bytes
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
public class rotatematrix90deg {
public static void main(String[] args) {
int arr[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
rotate(arr);
System.out.println("Rotated Image");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
public static void rotate(int matrix[][])
{
for (int i = 0; i < matrix.length; i++) {
for (int j = i; j < matrix[0].length; j++) {
int temp = 0;
temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
for (int i = 0; i < matrix.length; i++)
{
for (int j = 0; j < matrix.length / 2; j++)
{
int temp = 0;
temp = matrix[i][j];
matrix[i][j] = matrix[i][matrix.length - 1 - j];
matrix[i][matrix.length - 1 - j] = temp;
}
}
}
}