A matrix which is formed by turning all the rows into columns and vice versa is called a transpose of a matrix.

If a m x n matrix is taken as input, the order of the transposed matrix will be n x m.

Let us consider a 3×2 matrix(1 row, 2 columns)

1 2

3 4

5 6

The transpose of the matrix is given by

1 3 5

2 4 6

which is a 2×3 matrix(2 rows,1 column)

This program takes number of rows, columns and the elements of a matrix as input and returns the transpose of the given matrix as output. It uses a two dimensional array to represent the matrices.

You may also consider reading:

## Java Program to Transpose a Matrix

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]; int[][] transpose = new int[r]; 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:

{ 0 comments… add one now }