Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

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

  1. Start. Create a Scanner object sc.
  2. Read the number of rows m and the number of columns n.
  3. If m or n is less than 1 or more than 10, print an invalid size message and stop.
  4. Otherwise create the array a[m][n] for the matrix and the array t[n][m] for the transpose.
  5. Read the elements of a row by row:
    • the outer loop i runs from 0 to m - 1 (the rows);
    • the inner loop j runs from 0 to n - 1 (the columns) and stores sc.nextInt() in a[i][j].
  6. Build the transpose: for every i from 0 to m - 1 and every j from 0 to n - 1, copy a[i][j] into t[j][i].
  7. Print the original matrix a with m rows and n columns, one row per line.
  8. Print the transpose t with n rows and m columns, one row per line.
  9. 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

VariableData TypePurpose
argsString[]Command-line arguments of main (not used)
scScannerReads the size and the elements typed by the user
mintNumber of rows of the matrix
nintNumber of columns of the matrix
aint[][]Stores the original matrix of m rows and n columns
tint[][]Stores the transpose, with n rows and m columns
iintLoop counter for the row index
jintLoop 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.

One value per line

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:

ija[i][j]Statementt after the step
001t[0][0] = 1row 0: 1, -
012t[1][0] = 2row 1: 2, -
023t[2][0] = 3row 2: 3, -
104t[0][1] = 4row 0: 1, 4
115t[1][1] = 5row 1: 2, 5
126t[2][1] = 6row 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

  1. Symmetric matrix "Write a program to check whether a square matrix is symmetric, that is, equal to its transpose."
  2. Transpose in place "Transpose a square matrix without using a second array, by swapping a[i][j] and a[j][i]."
  3. Class version "Write a class with methods to accept, transpose and display a matrix, and a main that creates an object."