Program:
import java.util.Scanner;
class TransposeMatrix{
public static void main(String args[]){
int m,n,c,d;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix:");
m= sc.nextInt();
n= sc.nextInt();
int matrix[][] = new int[m][n];
System.out.println("Enter the elements of matrix:");
for ( c =0; c<m; c++)
for ( d =0; d<n; d++)
matrix[c][d] = sc.nextInt();
int transpose[][] = new int[n][m];
for(c=0; c<m; c++){
for(d=0; d<n; d++)
transpose[d][c] = matrix[c][d];
}
System.out.println("Transpose of eneterd matrix:- ");
for(c = 0; c<n; c++){
for(d=0; d <m; d++){
System.out.print(transpose[c][d] + " \t");
}
System.out.println("\n");
}
}
}
Answer:
Enter the number of rows and columns of matrix:
2
3
Enter the elements of matrix:
5
6
8
9
4
6
Transpose of eneterd matrix:-
5 9
6 4
8 6
0 Comments