126. JAVA Program to find Transpose of the given matrix.
How does this program work?
- In this program we are going to learn about how to find transpose of the given matrix using java.
- Transpose of a matrix is obtained by changing rows to columns and columns to rows of given matrix.
Here is the code
//To find Transpose of the given matrix
import java.util.Scanner;public class Traspose
{
public static void main(String args[])
{
int m, n, i, j;
System.out.println( "Enter the number of rows and columns of matrix");
Scanner obj = new Scanner(System.in);
m = obj.nextInt();
n = obj.nextInt();
int matrix[][] = new int[m][n];
System.out.println("Enter the elements of matrix");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
matrix[i][j] = obj.nextInt();
int transpose[][] = new int[n][m];
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
transpose[j][i] = matrix[i][j];
System.out.println("Transpose of the matrix:");
for (i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
System.out.print(transpose[i][j]+"\t");
System.out.print("\n");
}
}
}