Java program to find transpose of a matrix

Let’s learn java program to find transpose of a matrix.

Java program to find transpose of a matrix

Java program to display transpose matrix is one of the common interview question in java programming.

To find and print the transpose of a given matrix in java user enters elements of a matrix using scanner.

Also read – java program for matrix multiplication

Transpose of a matrix in java can be performed by converting columns of matrix to row and rows of matrix to columns.

For example:

3x3 matrix

a11    a12    a13
a21    a22    a23
a31    a32    a33

transposed 3x3 matrix

a11    a21    a31
a12    a22    a32
a13    a23    a33



Let’s see java program to display transpose a matrix,

import java.util.Scanner;

public class MatrixTransposeInJava 
{
   public static void main(String[] args) 
   { 
      int a, b;
      System.out.println("Please enter rows and columns : ");
      // transpose of a matrix in java using scanner
      Scanner sc = new Scanner(System.in);
      int row = sc.nextInt();
      int col = sc.nextInt();
      int arrNum[][] = new int[row][col];
      System.out.println("Please enter matrix elements : ");
      for(a = 0; a < row; a++)
      {
         for(b = 0; b < col; b++) 
         {
            arrNum[a][b] = sc.nextInt();
         }
      }
      System.out.println("Before transpose : ");
      for(a = 0; a < row; a++)
      {
         for(b = 0; b < col; b++)
         {
            System.out.print(arrNum[a][b] + " ");
         }
         System.out.println(" ");
      }
      System.out.println("After transpose : ");
      for(a = 0; a < col; a++)
      {
         for(b = 0; b < row; b++)
         {
            System.out.print(arrNum[b][a] + " ");
         }
         System.out.println(" ");
      }
      sc.close();
   }
}




Output:

java program to find transpose of a matrix

Print 3×3 matrix

Let’s learn java program to print 3×3 matrix. Here’s 3×3 matrix in java example,

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MatrixTransposeDemo 
{
   public static void main(String[] args) throws IOException 
   {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      int[][] inputNumber = new int[3][3];
      int a, b;
      String strMatrix;
      System.out.println("Please enter elements to print 3x3 matrix: ");
      for(a = 0; a <= 2; a++)
      {
         for(b = 0; b <= 2; b++)
         {
            strMatrix = br.readLine();
            inputNumber[a][b]=Integer.parseInt(strMatrix);
         }
      }
      System.out.println("Elements in 3x3 matrix are: ");
      System.out.println("");
      for(a = 0; a <= 2; a++)
      {
         for(b = 0; b <= 2; b++)
         {
            System.out.print(inputNumber[a][b] + "\t");
         }
         System.out.println();
      }
   }
}



Output:

Please enter elements to print 3×3 matrix:
2
4
6
8
1
3
5
7
9
Elements in 3×3 matrix are:

2 4 6
8 1 3
5 7 9

Trace of a matrix

Here we are going to learn java program to find trace of a matrix,

import java.util.Scanner;

public class TraceMatrixDemo 
{
   public static void main(String[] args) 
   {
      int[][] arrInput = new int[10][10];
      int a, b;
      double total = 0;
      System.out.println("Please enter total rows and columns: ");
      Scanner sc = new Scanner(System.in);
      int row = sc.nextInt();
      int column = sc.nextInt();
      System.out.println("Please enter matrix: ");
      for(a = 0; a < row; a++)
      {
         for(b = 0; b < column; b++) 
         {
            arrInput[a][b] = sc.nextInt();
            System.out.print(" ");
         }
      }
      System.out.println("Entered matrix is: ");
      for(a = 0; a < row; a++)
      {
         for(b = 0; b < column; b++)
         {
            System.out.print(arrInput[a][b] + " ");
         }
         System.out.println(" ");
      }
      System.out.println("Trace of a matrix: ");
      for(a = 0; a < row; a++)
      {  
         for(b = 0; b < column; b++)
         {
            if(a == b)
            {
               total = total + (arrInput[a][b]);
            }
         }
      }
      System.out.println(total);
      sc.close();
   }
}



Output:

Please enter total rows and columns:
3
3
Please enter matrix:
2
4
6
8
1
3
5
7
9
Entered matrix is:
2 4 6
8 1 3
5 7 9
Trace of a matrix:
12.0

Using bufferedreader

Now let’s learn transpose of a matrix in java using bufferedreader. Here’s java program to transpose a matrix,

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TransposeMatrixDemo 
{
   public static void main(String[] args) throws IOException 
   {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Please enter number of rows: ");
      int row = Integer.parseInt(br.readLine());
      System.out.print("Please enter number of columns: ");
      int col = Integer.parseInt(br.readLine());
      int[][] arrMatrix = new int[row][col];
      int[][] arrTranspose = new int[row][col];
      System.out.println("Please enter elements of matrix: ");
      for(int a = 0; a < row; a++) 
      {
         for(int b = 0; b < col; b++) 
         {
            arrMatrix[a][b] = Integer.parseInt(br.readLine());
         }
      }
      System.out.println("Given Matrix\n");
      for(int a = 0; a < row; a++) 
      {
         for(int b = 0; b < col; b++) 
         {
            System.out.print(arrMatrix[a][b] + " ");
         }
         System.out.print("\n");
      }
      for(int a = 0; a < row; a++) 
      {
         for(int b = 0; b < col; b++) 
         {
            arrTranspose[b][a] = arrMatrix[a][b];
         }
      }
      System.out.println("Transpose matrix in java \n");
      for(int a = 0; a < col; a++) 
      {
         for(int b = 0; b < row; b++) 
         {
            System.out.print(arrTranspose[a][b] + " ");
         }
         System.out.print("\n");
      }
   }
}



Output:

Please enter number of rows: 3
Please enter number of columns: 3
Please enter elements of matrix:
1
2
3
4
5
6
7
8
9
Given Matrix

1 2 3
4 5 6
7 8 9
Transpose matrix in java

1 4 7
2 5 8
3 6 9