import java.io.*;
public class SEVENTEEN{
public static void main(String[] args) throws Exception {
//declare all variables/matrices
int rows, cols;
int[][] matrix, tranMatrix;
//use BufferedReader to get the number of rows and columns in the matrix
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter number of rows: ");
rows = Integer.parseInt(reader.readLine());
System.out.print("Enter number of columns: ");
cols = Integer.parseInt(reader.readLine());
//set the size
matrix = new int[rows][cols];
tranMatrix = new int[cols][rows];
System.out.println("Enter elements for Matrix A");
for(int j = 0; j < rows; j++) {
for(int k = 0; k < cols; k++) {
matrix[j][k] = Integer.parseInt(reader.readLine());
}
}
//tranMatrix = matrix;
for(int h = 0; h < rows; h++) {
for(int i = 0; i < cols; i++){
tranMatrix[i][h] = matrix[h][i];
}
}
System.out.println("Matrix A is:");
for(int l = 0; l < rows; l++) {
for(int m = 0; m < cols; m++){
System.out.print(matrix[l][m]+" ");
}
System.out.println();
}
System.out.println("Tranpose matrix is: ");
for(int l = 0; l < cols; l++){
for(int m = 0; m < rows; m++){
System.out.print(tranMatrix[l][m]+" ");
}
System.out.println();
}
}
}
|