How to get transpose of matrix in java Program

import java.util.Scanner;
class TransposeAMatrix {
  public static void main(String args[]) {
    int m, n, c, d;

    // Create scanner object to take input
    Scanner in = new Scanner(System.in);

    // Taking number of rows and columns as input
    System.out.println("Enter the number of rows and columns of matrix");
    m = in.nextInt();
    n = in.nextInt();

    // Declare the matrix
    int matrix[][] = new int[m][n];

    // Taking elements of matrix as input
    System.out.println("Enter the elements of matrix");
    for (c = 0; c < m; c++) {
      for (d = 0; d < n; d++) {
        matrix[c][d] = in.nextInt();
      }
    }

    // Declare the transpose matrix
    int transpose[][] = new int[n][m];

    // Calculate the transpose of the matrix
    for (c = 0; c < m; c++) {
      for (d = 0; d < n; d++) {
        transpose[d][c] = matrix[c][d];
      }
    }

    // Display the transpose of the matrix
    System.out.println("Transpose of entered matrix:");
    for (c = 0; c < n; c++) {
      for (d = 0; d < m; d++) {
        System.out.print(transpose[c][d] + "\t");
      }
      System.out.print("\n");
    }

    // Close the scanner to prevent resource leak
    in.close();
  }
}

Step-by-Step Breakdown

1. Importing the Scanner Class:

import java.util.Scanner;

This imports the Scanner class to accept user input from the console.

2. Class and Main Method:

class TransposeAMatrix

{

     public static void main(String args[])

The program has been encapsulated in the TransposeAMatrix class.

The execution of the program starts from the main () method.

3. Variable Declaration:

int m, n, c, d;

Scanner in = new Scanner(System.in);

  • The variable declarations for m and n describe the number of rows and columns of the matrix.
  • c and d are used as loop variables during the traversal of a matrix.
  • Scanner in = new Scanner(System.in); it is a Scanner object which takes the input of values.

4. Input Matrix Dimensions

System.out.println(“Enter the number of rows and columns of matrix”);

m = in.nextInt();

n = in.nextInt();

Ask the user for row length, columns, etc: Number of columns(m), m.

5. Input Matrix Elements:

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] = in.nextInt();

            }

    }

  • Declare a 2D array matrix[][] to hold the elements of the matrix. The dimensions of the matrix are m-by-n elements.
  • The elements of the matrix would be requested from the user using nested for loops, that are stored in the array matrix.

6. Transpose of the Matrix:

int transpose[][] = new int[n][m];

for (c = 0; c < m; c++)

{

   for (d = 0; d < n; d++)

       transpose[d][c] = matrix[c][d];

}

  • A new 2D array transpose[][] is declared of size n x m, where the columns of the original matrix are turned into rows of the transposed matrix, and vice versa.
  • The nested for loops fill the transpose[][] matrix:
  • The value at matrix[c][d] is assigned to transpose[d][c]. This will swap the rows and columns, meaning the matrix has been transposed.

7. Print Transpose Matrix:

System.out.println(“Transpose of entered matrix:-“);

for (c = 0; c < n; c++)

{

   for (d = 0; d < m; d++)

       System.out.print(transpose[c][d] + “t”);

   System.out.print(” “);

}

The program that prints the transpose matrix.

  • The outer loop (c) loops over the rows of the transposed matrix.
  • The inner loop (d) iterates over the columns of the transposed matrix.
  • Each element of the transposed matrix is displayed with a tab space for better readability and a new line after each row.

Sample Input and Output:

Input 1:

Enter the number of rows and columns of matrix

2 3

Enter the elements of matrix

1 2 3

4 5 6

The given matrix is:

 1  2  3

 4  5  6

Transpose of the matrix is obtained by interchanging rows with columns:

 1  4

 2  5

 3  6

Input 2:

Enter the number of rows and columns of matrix

3 2

Enter the elements of matrix

1 2

3 4

5 6

Output:

Transpose of input matrix:-

1 3 5

2 4 6

Explanation:

Original matrix is:

 1  2

 3  4

 5  6

Transpose of the matrix is:

 1  3  5

 2  4  6

Leave a Comment

Your email address will not be published. Required fields are marked *