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. using Object Class and Driver Class
import java.util.Scanner;
class ObtainTranspose{
int[][] arrT = new int[4][4];
public void arrayTranspose (int[][] arr){
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
arrT[j][i] = arr[i][j];
}
}
}
public void printArray(){
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
System.out.printf("%5d", arrT[i][j]);
}
System.out.println("");
}
}
}
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
ObtainTranspose obTra = new ObtainTranspose();
int[][] arr = {{85, 42, 11, 47}, {19, 2, 43, 7}, {1, 77, 54, 32}, {12, 22, 24, 26}};
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
System.out.printf("%5d", arr[i][j]);
}
System.out.println("");
}
System.out.println("");
obTra.arrayTranspose(arr);
obTra.printArray();
}
}
Comments
Leave a comment