As we know very well matrix in programming language is represented by 2 dimensional array. So we are providing the java source code which first ask about the no of rows and columns and then input all the elements of the matrix row wise. After that it transposes the i/p matrix.
TransposeMatrix.java:
--------------------------------
package in.anyforum;
import java.io.*;
class TranspMatrix
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int[][] a=new int[5][5];
int i,j,p,q;
System.out.println("Enter order of matrix A: ");
p=Integer.parseInt(br.readLine());
q=Integer.parseInt(br.readLine());
System.out.println("Enter the elements of matrix A:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
a[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("The matrix A is:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
System.out.print("\t"+a[i][j]);
}
System.out.println();
}
System.out.println("The transpose matrix A is:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
System.out.print("\t"+a[j][i]);
}
System.out.println();
}
}
}
************************************* Input & Output *************************************
Enter order of matrix A:
4
4
Enter the elements of matrix A:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
The matrix A is:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The transpose matrix A is:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
5