Sponsered Links
Categories
Sponsered Links

Write a program TO INPUT A MATRIX and PRINT ITS TRANSPOSE

Write a program TO INPUT A MATRIX and PRINT ITS TRANSPOSE

import java.io.*;
 
 public class SEVENTEEN{
    public static void main(String[] args) throws Exception    {
        //declare all variables/matrices
        int rows, cols;
        int[][] matrix, tranMatrix;   
         //use BufferedReader to get the number of rows and columns in the matrix
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter number of rows: ");
        rows = Integer.parseInt(reader.readLine());
        System.out.print("Enter number of columns: ");
        cols = Integer.parseInt(reader.readLine());
        //set the size
        matrix = new int[rows][cols];
        tranMatrix = new int[cols][rows];
        System.out.println("Enter elements for Matrix A");
        for(int j = 0; j < rows; j++)        {   
            for(int k = 0; k < cols; k++)            {   
                matrix[j][k] = Integer.parseInt(reader.readLine());
            }
        }       
          //tranMatrix = matrix;
         for(int h = 0; h < rows; h++)    {   
            for(int i = 0; i < cols; i++){
                tranMatrix[i][h] = matrix[h][i];
            }
        }
         System.out.println("Matrix A is:");
        for(int l = 0; l < rows; l++)        {   
            for(int m = 0; m < cols; m++){
                System.out.print(matrix[l][m]+" ");   
            }
            System.out.println();
        }       
        System.out.println("Tranpose matrix is: ");
               
        for(int l = 0; l < cols; l++){   
            for(int m = 0; m < rows; m++){
                System.out.print(tranMatrix[l][m]+" ");   
            }
            System.out.println();
        }       
    }
}

Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2009 JSPSERVLETTUTORIAL.INFO All Right Reserved