public class Transpose {
public static void main(String[] args) {
int [][] M = {{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}
};
System.out.println("Original Square Matrix: ");
printMatrix(M);
transposeMatrix(M);
}
public static void transposeMatrix(int [][]M){
int row = M.length;
int colum = M[0].length;
for(int i = 0; i < colum ;i++){
for(int j = 0; j<(row/2) ; j++){
int save = M[j][i];
M[j][j] = M[colum - j -1][i];
M[colum - j -1][i] = save;
}
}
printMatrix(M);
}
public static void printMatrix(int [][]M){
int r = M.length;
int c = M[0].length;
for(int i = 0; i< r; i++){
for(int j = 0 ;j < c ; j++ ){
System.out.println(M[i][j] + " ");
}
System.out.println();
}
}
}