Java Program To Transpose Given Matrix
What is Matrix Transposition?
Ans: Matrix Transposition is a process of converting rows into columns and columns into rows and vice versa of given Matrix.
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
- Start
- 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].
- 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.
- 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?
Related Topics:
- Arithmetic Operations Java Program
- Java program to perform Logical and Relational operations
- Java program to Implement Mathematical functions defined in Java Math class
- Java basic Programs with Explanation
- Java Program to find Min,Max Element in an Array
- Java Program to sort Array Elements in ascending and descending order
- Java Program to display Array item at Even & Odd position
- Program to Calculate Sum and Average of Array elements
- Java Program to find Index of specific Element in an array
- Program to find common elements between two arrays
- Java Program to Add/Insert specific Element to an Array
- Java Program to delete specific element from an Array
- Program to calculate frequency of each element in Array
- Java Program to find Even and Odd Numbers in the arrays
- Java Program to search Given Number in an Array
- Java Program to Add or Sub Two Matrices
- Java Program to Multiply two or More Matrices
- Java Program to Print Array in Reverse Order
- Java Program To Transpose Given Matrix
- Java Programs List- Java Programs Examples With Output