Write a program to transpose a Matrix.
package programs;
import java.util.Scanner;
public class TransposeMatrix {
public static void main(String[] args) {
int row, col, i, j;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix");
row = in.nextInt();
col = in.nextInt();
int mtrx[][] = new int[row][col];
System.out.println("Enter the elements of matrix");
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
mtrx[i][j] = in.nextInt();
System.out.println("Original Entered matrix :-");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++)
System.out.print(mtrx[i][j] + "\t");
System.out.print("\n");
}
int transpose[][] = new int[col][row];
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++)
transpose[j][i] = mtrx[i][j];
}
System.out.println("Transposed matrix :-");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++)
System.out.print(transpose[i][j] + "\t");
System.out.print("\n");
}
}
}
package programs;
import java.util.Scanner;
public class TransposeMatrix {
public static void main(String[] args) {
int row, col, i, j;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix");
row = in.nextInt();
col = in.nextInt();
int mtrx[][] = new int[row][col];
System.out.println("Enter the elements of matrix");
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
mtrx[i][j] = in.nextInt();
System.out.println("Original Entered matrix :-");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++)
System.out.print(mtrx[i][j] + "\t");
System.out.print("\n");
}
int transpose[][] = new int[col][row];
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++)
transpose[j][i] = mtrx[i][j];
}
System.out.println("Transposed matrix :-");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++)
System.out.print(transpose[i][j] + "\t");
System.out.print("\n");
}
}
}