# Java Program to find the transpose of a given matrix

In this program, we need to find the transpose of the given matrix and print the resulting matrix.

### Transpose of Matrix

Transpose of a matrix can be found by interchanging rows with the column that is, rows of the original matrix will become columns of the new matrix. Similarly, columns in the original matrix will become rows in the new matrix. The operation can be represented as follows:

If the dimension of the original matrix is 2 × 3 then, the dimensions of the new transposed matrix will be 3 × 2.

1. [ AT ]<sub>ij</sub> = [ A ]<sub>ji</sub>

### Algorithm

• STEP 1: START
• STEP 2: DEFINE rows, cols
• STEP 3: INITIALIZE matrix a[][] ={{1, 2, 3},{4, 5, 6}, {7, 8, 9}}
• STEP 4: rows = a.length
• STEP 5: cols = a.length
• STEP 6: t[][] = [cols][rows]
• STEP 7: REPEAT STEP 8 to STEP 9 UNTIL i<cols
// for(i=0; i<cols; i++)
• STEP 8: REPEAT STEP 9 UNTIL j<rows
// for(j=0; j<rows; j++)
• STEP 9: t[i][j] = a[j][i]
• STEP 10: PRINT “Transpose of given matrix”
• STEP 11: REPEAT STEP 12 to STEP 14 UNTIL i<cols
//for(i=0; i<cols; i++)
• STEP 12: REPEAT STEP 13 UNTIL j<rows
//for(j=0; j<rows; j++)
• STEP 13: PRINT t[i][j]
• STEP 14: PRINT new line
• STEP 15: END

## Program

1. public class Transpose
2. {
3.     public static void main(String[] args) {
4.         int rows, cols;
5.
6.         //Initialize matrix a
7.           int a[][] = {
8.                           {1, 2, 3},
9.                           {4, 5, 6},
10.                           {7, 8, 9}
11.                        };
12.
13.           //Calculates number of rows and columns present in given matrix
14.           rows = a.length;
15.         cols = a.length;
16.
17.         //Declare array t with reverse dimensions
18.         int t[][] = new int[cols][rows];
19.
20.         //Calculates transpose of given matrix
21.         for(int i = 0; i < cols; i++){
22.             for(int j = 0; j < rows; j++){
23.                 //Converts the row of original matrix into column of transposed matrix
24.                 t[i][j] = a[j][i];
25.             }
26.         }
27.
28.         System.out.println(“Transpose of given matrix: “);
29.         for(int i = 0; i < cols; i++){
30.             for(int j = 0; j < rows; j++){
31.                System.out.print(t[i][j] + ” “);
32.             }
33.             System.out.println();
34.         }
35.     }
36. }

Output:

```Transpose of given matrix
1   4  7
2   5  8
3   6  9 ```