Write a Java program to obtain transpose of a 4 x 4 matrix. The Transpose of the matrix is obtained by exchanging the elements of each row. With the elements of the corresponding column.
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int[][] arr = {{1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}};
int[][] arrT = new int[4][4];
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
arrT[j][i] = arr[i][j];
}
}
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
System.out.print(arrT[i][j] + " ");
}
System.out.println("");
}
}
}
Comments
Leave a comment