-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixRotation.java
More file actions
50 lines (46 loc) · 926 Bytes
/
Copy pathMatrixRotation.java
File metadata and controls
50 lines (46 loc) · 926 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class MatrixRotation {
public static void main(String[] args)
{
int matrixSize=4;
//Preparing the input matrix
int[][] matrix=new int[matrixSize][matrixSize];
int value=1;
for(int i=0;i<matrixSize;i++)
{
for(int j=0;j<matrixSize;j++)
{
matrix[i][j]=value;
value++;
}
}
displayMatrix(matrix,matrixSize);
int[][] output=rotateMatrix(matrix,matrixSize);
System.out.println("After rotation by 90 degrees");
displayMatrix(output, matrixSize);
}
public static int[][] rotateMatrix(int[][] matrix,int matrixSize)
{
int[][] outputMatrix=new int[matrixSize][matrixSize];
int k=0;
for(int i=matrixSize-1;i>-1;i--)
{
for(int j=0;j<matrixSize;j++)
{
outputMatrix[k][j]=matrix[j][i];
}
k++;
}
return outputMatrix;
}
public static void displayMatrix(int[][] matrix,int size)
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
System.out.print(matrix[i][j]+ " ");
}
System.out.println();
}
}
}