transpose of a matrix in java using bufferedreader

Solutions on MaxInterview for transpose of a matrix in java using bufferedreader by the best coders in the world

showing results for - "transpose of a matrix in java using bufferedreader"
Daniela
20 Jun 2020
1// Transpose of a matrix in java using BufferedReader
2import java.io.BufferedReader;
3import java.io.IOException;
4import java.io.InputStreamReader;
5public class TransposeMatrixDemo 
6{
7   public static void main(String[] args) throws IOException 
8   {
9      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10      System.out.print("Please enter number of rows: ");
11      int row = Integer.parseInt(br.readLine());
12      System.out.print("Please enter number of columns: ");
13      int col = Integer.parseInt(br.readLine());
14      int[][] arrMatrix = new int[row][col];
15      int[][] arrTranspose = new int[row][col];
16      System.out.println("Please enter elements of matrix: ");
17      for(int a = 0; a < row; a++) 
18      {
19         for(int b = 0; b < col; b++) 
20         {
21            arrMatrix[a][b] = Integer.parseInt(br.readLine());
22         }
23      }
24      System.out.println("Given Matrix\n");
25      for(int a = 0; a < row; a++) 
26      {
27         for(int b = 0; b < col; b++) 
28         {
29            System.out.print(arrMatrix[a][b] + " ");
30         }
31         System.out.print("\n");
32      }
33      for(int a = 0; a < row; a++) 
34      {
35         for(int b = 0; b < col; b++) 
36         {
37            arrTranspose[b][a] = arrMatrix[a][b];
38         }
39      }
40      System.out.println("Transpose matrix in java \n");
41      for(int a = 0; a < col; a++) 
42      {
43         for(int b = 0; b < row; b++) 
44         {
45            System.out.print(arrTranspose[a][b] + " ");
46         }
47         System.out.print("\n");
48      }
49   }
50}