Monday, 12 January 2015

Java Program to find the transpose of a given matrix

This is a Java Program which prints the transpose of a given matrix.

Transpose of a matrix is obtained by interchanging the rows and columns of  a given matrix.

see the figure below,

PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class Transpose {
public static void main(String[] args) {
int rows, columns, i, j;
Scanner scan = new Scanner(System.in);
System.out.print("Enter order of matrix : \t");
rows = scan.nextInt();
columns = scan.nextInt();
int matrix[][] = new int[rows][columns];
System.out.print("Enter elements of the matrix :\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
System.out.print("Enter element m" + (i + 1) + (j + 1) + ":\t");
matrix[i][j] = scan.nextInt();
}
}
scan.close();
System.out.print("\n\nMATRIX is :\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++)
System.out.print(matrix[i][j] + "\t");

System.out.print("\n");
}
System.out.print("\nIts TRANSPOSE is:\n");
for (j = 0; j < columns; j++) {
for (i = 0; i < rows; i++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.print("\n");
}
}
}

OUTPUT : 

No comments:

Post a Comment