import java.io.*; class Transpose { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("\nEnter the Number of Rows : "); int r = Integer.parseInt(br.readLine()); System.out.print("\nEnter the Number of Columns : "); int c = Integer.parseInt(br.readLine()); int[][] matrix = new int[r][c]; int[][] transpose = new int[r][c]; System.out.println("\nEnter the Elements of Matrix : "); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { matrix[i][j] = Integer.parseInt(br.readLine()); } } System.out.println("\nGiven Matrix\n"); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { System.out.print(matrix[i][j] + " "); } System.out.print("\n"); } for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { transpose[j][i] = matrix[i][j]; } } System.out.println("\nTranspose Matrix\n"); for (int i = 0; i < c; i++) { for (int j = 0; j < r; j++) { System.out.print(transpose[i][j] + " "); } System.out.print("\n"); } } }
OUTPUT:
Enter the Number of Rows : 2 Enter the Number of Columns : 2 Enter the Elements of Matrix : 5 6 7 8 Given Matrix 5 6 7 8 Transpose Matrix 5 7 6 8