Rotate the matrix by 90 degree in java

We are rotating the content of a matrix by 90 degree.

Assumptions

a is the given matrix.

We will just print it in the rotated form.

///code

int a[][] = {{1,2,3,4,5,6},{7,8,9,10,11,12}};

//no. of column
int m = a[0].length;

//no. of rows
int n = a.length;

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
for (int i = 0; i < m; i++) {
for (int j = n-1; j >=0; j–) {
System.out.print(a[j][i]+” .. “);
}
System.out.println();
}

Advertisements