Advertisement
exqxste

Java Program to Find Transpose of a Matrix

Jul 10th, 2020
1,332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. // Made by CelestialTwist
  2.  
  3. public class Transpose {
  4.  
  5.     public static void main(String[] args) {
  6.         int row = 2, column = 3;
  7.         int[][] matrix = { {2, 3, 4}, {5, 6, 4} };
  8.  
  9.         // Display current matrix
  10.         display(matrix);
  11.  
  12.         // Transpose the matrix
  13.         int[][] transpose = new int[column][row];
  14.         for(int i = 0; i < row; i++) {
  15.             for (int j = 0; j < column; j++) {
  16.                 transpose[j][i] = matrix[i][j];
  17.             }
  18.         }
  19.  
  20.         // Display transposed matrix
  21.         display(transpose);
  22.     }
  23.  
  24.     public static void display(int[][] matrix) {
  25.         System.out.println("The matrix is: ");
  26.         for(int[] row : matrix) {
  27.             for (int column : row) {
  28.                 System.out.print(column + "    ");
  29.             }
  30.             System.out.println();
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement