import java.io.*;
public class TransposeMatrix
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a[][], b[][];
int i, j;
// Read Rows and Columns of Matrix from User
System.out.print("Enter Number of Rows:: ");
int m = Integer.parseInt(br.readLine());
System.out.print("Enter Number of Columns:: ");
int n = Integer.parseInt(br.readLine());
a = new int[m][n]; // Original Matrix
b = new int[n][m]; // Transpose Matrix
// Read Matrix Elements from User
System.out.println("Enter Matrix Data::");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
System.out.print("Enter Element:: ");
a[i][j] = Integer.parseInt(br.readLine());
}
}
// Displaying Matrix
System.out.println("\nOriginal Matrix is::");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
System.out.print("\t" + a[i][j]);
}
System.out.println();
}
// Perform Transpose
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
b[j][i] = a[i][j];
// Display Transpose Matrix
System.out.println("\nTranspose Matrix is::");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
System.out.print("\t" + b[i][j]);
}
System.out.println();
}
}
}
public class TransposeMatrix
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a[][], b[][];
int i, j;
// Read Rows and Columns of Matrix from User
System.out.print("Enter Number of Rows:: ");
int m = Integer.parseInt(br.readLine());
System.out.print("Enter Number of Columns:: ");
int n = Integer.parseInt(br.readLine());
a = new int[m][n]; // Original Matrix
b = new int[n][m]; // Transpose Matrix
// Read Matrix Elements from User
System.out.println("Enter Matrix Data::");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
System.out.print("Enter Element:: ");
a[i][j] = Integer.parseInt(br.readLine());
}
}
// Displaying Matrix
System.out.println("\nOriginal Matrix is::");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
System.out.print("\t" + a[i][j]);
}
System.out.println();
}
// Perform Transpose
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
b[j][i] = a[i][j];
// Display Transpose Matrix
System.out.println("\nTranspose Matrix is::");
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
System.out.print("\t" + b[i][j]);
}
System.out.println();
}
}
}
OUTPUT::
Enter Number of Rows:: 2
Enter Number of Columns:: 3
Enter Matrix Data::
Enter Element:: 1
Enter Element:: 2
Enter Element:: 3
Enter Element:: 4
Enter Element:: 5
Enter Element:: 6
Original Matrix is::
1 2 3
4 5 6
Transpose Matrix is::
1 4
2 5
3 6