ISC Practicals and Theory - Transpose
Updated 20 Oct 2026
The transpose of a matrix is the matrix you get by turning its rows into columns. This program reads an m x n matrix into a two-dimensional array, builds its transpose in a second array and prints both.
Definition
The transpose of a matrix $A$ with $m$ rows and $n$ columns is the matrix $A^T$ with $n$ rows and $m$ columns, in which the element in row $i$, column $j$ of $A$ becomes the element in row $j$, column $i$ of $A^T$:
t[j][i] = a[i][j]
So the first row of $A$ becomes the first column of $A^T$, the second row becomes the second column, and so on. For example, a $2 \times 3$ matrix has a $3 \times 2$ transpose:
- $A$ has rows $1\ 2\ 3$ and $4\ 5\ 6$.
- $A^T$ has rows $1\ 4$, $2\ 5$ and $3\ 6$.
A square matrix has a transpose of the same size.
Algorithm
- Start. Create a
Scannerobjectsc. - Read the number of rows
mand the number of columnsn. - If
mornis less than 1 or more than 10, print an invalid size message and stop. - Otherwise create the array
a[m][n]for the matrix and the arrayt[n][m]for the transpose. - Read the elements of
arow by row:- the outer loop
iruns from 0 tom - 1(the rows); - the inner loop
jruns from 0 ton - 1(the columns) and storessc.nextInt()ina[i][j].
- the outer loop
- Build the transpose: for every
ifrom 0 tom - 1and everyjfrom 0 ton - 1, copya[i][j]intot[j][i]. - Print the original matrix
awithmrows andncolumns, one row per line. - Print the transpose
twithnrows andmcolumns, one row per line. - Stop.
Program
Everything is written in main of the class Transpose: the size is checked first, then the matrix is read, transposed and displayed with nested loops.
import java.util.Scanner;
public class Transpose
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows (m): ");
int m = sc.nextInt();
System.out.print("Enter the number of columns (n): ");
int n = sc.nextInt();
if (m < 1 || m > 10 || n < 1 || n > 10)
{
System.out.println("Invalid size. Rows and columns must be from 1 to 10.");
}
else
{
int a[][] = new int[m][n];
int t[][] = new int[n][m];
System.out.println("Enter " + (m * n) + " elements row by row:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = sc.nextInt();
}
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
t[j][i] = a[i][j];
}
}
System.out.println("Original matrix (" + m + " x " + n + "):");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
System.out.println("Transpose (" + n + " x " + m + "):");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
System.out.print(t[i][j] + "\t");
}
System.out.println();
}
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command-line arguments of main (not used) |
sc | Scanner | Reads the size and the elements typed by the user |
m | int | Number of rows of the matrix |
n | int | Number of columns of the matrix |
a | int[][] | Stores the original matrix of m rows and n columns |
t | int[][] | Stores the transpose, with n rows and m columns |
i | int | Loop counter for the row index |
j | int | Loop counter for the column index |
Sample input and output
Sample 1
Enter the number of rows (m): 2
Enter the number of columns (n): 3
Enter 6 elements row by row:
1 2 3
4 5 6
Original matrix (2 x 3):
1 2 3
4 5 6
Transpose (3 x 2):
1 4
2 5
3 6 Sample 2
Enter the number of rows (m): 3
Enter the number of columns (n): 3
Enter 9 elements row by row:
1 2 3
4 5 6
7 8 9
Original matrix (3 x 3):
1 2 3
4 5 6
7 8 9
Transpose (3 x 3):
1 4 7
2 5 8
3 6 9 Sample 3
Enter the number of rows (m): 0
Enter the number of columns (n): 4
Invalid size. Rows and columns must be from 1 to 10.Dry run
Take the input $m = 2$, $n = 3$ with rows $1\ 2\ 3$ and $4\ 5\ 6$. The array t is created with 3 rows and 2 columns. Each step of the copying loop is:
| i | j | a[i][j] | Statement | t after the step |
|---|---|---|---|---|
| 0 | 0 | 1 | t[0][0] = 1 | row 0: 1, - |
| 0 | 1 | 2 | t[1][0] = 2 | row 1: 2, - |
| 0 | 2 | 3 | t[2][0] = 3 | row 2: 3, - |
| 1 | 0 | 4 | t[0][1] = 4 | row 0: 1, 4 |
| 1 | 1 | 5 | t[1][1] = 5 | row 1: 2, 5 |
| 1 | 2 | 6 | t[2][1] = 6 | row 2: 3, 6 |
The transpose printed is the $3 \times 2$ matrix with rows $1\ 4$, $2\ 5$ and $3\ 6$, which matches the first sample run.
Variations
- Symmetric matrix "Write a program to check whether a square matrix is symmetric, that is, equal to its transpose."
- Transpose in place "Transpose a square matrix without using a second array, by swapping
a[i][j]anda[j][i]." - Class version "Write a class with methods to accept, transpose and display a matrix, and a
mainthat creates an object."