Java Program To Transpose Given Matrix

Java Program To Transpose Given Matrix

What is Matrix Transposition?

Consider a Given Matrix  1 2 3
                         4 5 6
                         7 8 9

After Transposition      1 4 7
                         2 5 8
                         3 6 9

Java Program To Transpose Given Matrix

In This Topic we are going to take two Examples. In Example-1 We will take Static Inputs(pre- defined Inputs). And In Example-2 We will take User defined Inputs.

Algorithm/Program Explanation

  1. Start
  2. Initialize two 2D-arrays
    • int matrix[][]={{1,2,3},{4,5,6},{7,8,9}}//add entries according to your convenience.
    • int transpose[][]=new int[3][3].
  3. Define two For Loops
    • for(int i=0;i<3;i++)
    • for (int j = 0; j < 3; j++)
    • Condition:
      • transpose[i][j] = matrix[j][i];// Transpose Logic
    • Print Original Matrix First.
    • Print Transpose Matrix.
  4. End

Example-1 : Taking Static Inputs

/*
Java Program To Transpose Matrix */
import java.util.*;
public class MatrixTranspose{
 public static void main(String args[]){
  int matrix[][]={{1,2,3},{4,5,6},{7,8,9}};
  int transpose[][]=new int[3][3];
  for(int i=0;i<3;i++) {
   for (int j = 0; j < 3; j++) {
    transpose[i][j] = matrix[j][i]; //Transpose Logic
   }

  }
   System.out.println("Matrix Before Transposition:");
   for(int i = 0; i<3; i++) {
    for (int j = 0; j < 3; j++) {
     System.out.print(matrix[i][j] + " ");
    }
    System.out.println();
   }
   System.out.println("Matrix After Transposition:");
   for(int i = 0; i<3; i++) {
    for (int j = 0; j < 3; j++)
     {
      System.out.print(transpose[i][j] + " ");
     }
     System.out.println();

   }
   }
  }
Output:
Matrix Before Transposition:
1 2 3 
4 5 6 
7 8 9 
Matrix After Transposition:
1 4 7 
2 5 8 
3 6 9 

Example-2 Taking User Defined Inputs

/*
Java Program To Transpose Matrix */
import java.util.*;
public class MatrixTranspose{
 public static void main(String args[]){
  Scanner sc=new Scanner(System.in);
  int matrix[][]= new int [3][3];
  int transpose[][]=new int[3][3];
  System.out.println("Enter The Elements in Matrix");
  for(int i=0;i<3;i++) {
   for (int j = 0; j < 3; j++) {
  matrix[i][j]=sc.nextInt();
   }

  }
  for(int i=0;i<3;i++) {
   for (int j = 0; j < 3; j++) {
    transpose[i][j] = matrix[j][i];
   }

  }
   System.out.println("Matrix Before Transposition:");
   for(int i = 0; i<3; i++) {
    for (int j = 0; j < 3; j++) {
     System.out.print(matrix[i][j] + " ");
    }
    System.out.println();
   }
   System.out.println("Matrix After Transposition:");
   for(int i = 0; i<3; i++) {
    for (int j = 0; j < 3; j++)
     {
      System.out.print(transpose[i][j] + " ");
     }
     System.out.println();

   }
   }
  }
Output:
Enter The Elements in Matrix
1 2 3
4 5 6
7 8 9
Matrix Before Transposition:
1 2 3 
4 5 6 
7 8 9 
Matrix After Transposition:
1 4 7 
2 5 8 
3 6 9 

Question Asked in this Topic:

  • Derive a java logic to transpose matrix?
  • Given a 2d array, print transpose of it?
  • Take user defined matrix, also code for transposing it?
  • Write a Program to implement Transpose Matrix?

Home

Related Topics:

Leave a Comment