Friday, January 30, 2015

Java program to convert a matrix into transpose matrix

The java program given below will create a matrix after getting all data from the user. Later the matrix will be converted to a transpose matrix. Transpose matrix is a matrix where the rows of a matrix become the columns to the new matrix.

After running the program you will be asked to provide the number of rows and columns one by one. Then you will be asked to provide the data for the matrix and soon the matrix will be formed and displayed.

Besides, the matrix will be converted to a transpose matrix and it will be displayed.
transpose matrix,converting matrix
import java.util.*;
public class Matrix{
 public static void main(String [] args){
  //creating scanner object
  Scanner input = new Scanner(System.in);

  //getting input for the number of rows
  System.out.print("Enter the number of Rows: ");
  int row = input.nextInt();

  //getting input for the number of columns
  System.out.print("Enter the number of Columns: ");
  int col = input.nextInt();

  //creating double array
  int matrix[][] = new int[30][30];

  //getting input into the array
  System.out.print ("Enter data for first Matrix:\n");
  for(int i=1; i<=row; i++){
   for(int j=1; j<=col; j++){
    matrix[i][j] = input.nextInt();
   }
  }

  //displaying the matrix
  System.out.println("The Matrix is:\n");
  for(int i=1; i<=row; i++){
   for(int j=1; j<=col; j++){
    System.out.print(matrix[i][j]+"   ");
   } System.out.print("\n\n\n");
  }

  //converting transpose matrix
  System.out.println("The Transpose Matrix is:\n");
  for(int i=1; i<=col; i++){
   for(int j=1; j<=row; j++){
    System.out.print(matrix[j][i]+"   ");
   } System.out.print ("\n\n\n");
  }
 }
}


No comments:

Post a Comment