Dark Mode On/Off

# Java Program To Display the Transpose of a Matrix

In this tutorial, we will learn how to display the transpose of a matrix. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.

Input: Enter the Matrix Elements:

1 2 3

4 5 3

9 3 2

Output: Transpose Matrix is:

1 4 3

2 5 3

3 3 2

## Program 1: Display the Transpose of a Matrix

In this program, we will use a separate matrix to store the transpose elements.

### Algorithm

1. Start
2. Declare variables for matrix rows and columns.
3. Ask the user to initialize the rows and columns.
4. Declare a matrix.
5. Ask the user to initialize the matrix elements.
6. Print the original matrix.
7. Declare another matrix that will store the transpose matrix element.
8. Store the elements in the transpose matrix by altering the rows and columns of the original matrix.
9. Display the transpose matrix.
10. Stop.

Below is the code for the same.

The below program demonstrates how to find the transpose of a matrix.

``````/*JAVA PROGRAM TO DISPLAY THE TRANSPOSE OF A MATRIX*/
import java.util.*;

public class Main
{
public static void main(String []args)
{
///Take input from the user
Scanner sc=new Scanner(System.in);

int m,n;                 //Matrix Size Declaration

System.out.println("Enter the number of rows: \n");
m=sc.nextInt();  //Matrix Size Initialization

System.out.println("Enter the number of column: \n");
n=sc.nextInt();  //Matrix Size Initialization

int arr[][]=new int[10][10];        //Matrix Size Declaration
System.out.println("Enter the elements of the matrix: ");
for(int i=0;i<m;i++)    //Matrix Initialization
{
for(int j=0;j<n;j++)
{
arr[i][j]=sc.nextInt();
}
}

//Print the original Matrix
System.out.println("The elements in the original matrix are: ");
for(int i=0;i<m;i++)     //Print the matrix
{
for(int j=0;j<n;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println("");
}

int brr[][]=new int[10][10];        //Transpose Matrix Declaration
for(int i=0;i<m;i++)     //Transpose Matrix initialization
{
for(int j=0;j<n;j++)
{
brr[j][i]=arr[i][j];     //Store elements in the transpose matrix
}
}

System.out.println("After transposing the elements are...");
for(int i=0;i<m;i++)      //Print the transpose matrix
{
for(int j=0;j<n;j++)
{
System.out.print(brr[i][j]+" ");
}
System.out.println("");
}

}
}
``````

Enter the number of rows: 3
Enter the number of column: 3
Enter the elements of the matrix: 5 4 3 1 2 6 9 8 7
The elements in the original matrix are:
5 4 3
1 2 6
9 8 7
After transposing the elements are...
5 1 9
4 2 8
3 6 7

## Program 2: Display the Transpose of a Matrix

In this program, we will use the same matrix but while printing we will alter the position of the elements.

### Algorithm

1. Start
2. Declare variables for matrix rows and columns.
3. Ask the user to initialize the rows and columns.
4. Declare a matrix.
5. Ask the user to initialize the matrix elements.
6. Print the original matrix.
7. To print the transpose matrix, alter the row and column positions.
8. Display the matrix.
9. Stop

Below is the code for the same.

The below program demonstrates how to display the transpose of a matrix without using any other array.

``````/*JAVA PROGRAM TO DISPLAY THE TRANSPOSE OF A MATRIX*/
import java.util.*;

public class Main
{
public static void main(String []args)
{
///Take input from the user
Scanner sc=new Scanner(System.in);

int m,n;                 //Matrix Size Declaration

System.out.println("Enter the number of rows: \n");
m=sc.nextInt();  //Matrix Size Initialization

System.out.println("Enter the number of column: \n");
n=sc.nextInt();  //Matrix Size Initialization

int arr[][]=new int[10][10];        //Matrix Size Declaration

System.out.println("Enter the elements of the matrix: ");
for(int i=0;i<m;i++)    //Matrix Initialization
{
for(int j=0;j<n;j++)
{
arr[i][j]=sc.nextInt();
}
}

//Print the original Matrix
System.out.println("The elements in the original matrix are: ");
for(int i=0;i<m;i++)     //Print the matrix
{
for(int j=0;j<n;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println("");
}

//Print the transpose matrix without creating any new matrix
System.out.println("After transposing the elements are...");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(arr[j][i]+" ");
}
System.out.println("");
}

}
}
``````

Enter the number of rows: 3
Enter the number of column: 3
Enter the elements of the matrix: 9 8 7 6 7 2 3 1 3
The elements in the original matrix are:
9 8 7
6 7 2
3 1 3
After transposing the elements are...
9 6 3
8 7 1
7 2 3

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.