Q:

Java Program to transpose a matrix

0

Program to transpose a matrix by getting input from the user using for loop

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.Scanner;
 
class demo1
{
   public static void main(String args[])
   {
      int m, n, c, d;
 
      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of rows and columns of matrix");
      m = in.nextInt();
      n = in.nextInt();
 
      int matrix[][] = new int[m][n];
 
      System.out.println("Enter the elements of matrix");
 
      for (c = 0; c < m; c++)
         for (d = 0; d < n; d++)
            matrix[c][d] = in.nextInt();
 
      int transpose[][] = new int[n][m];
 
      for (c = 0; c < m; c++)
         for (d = 0; d < n; d++)               
            transpose[d][c] = matrix[c][d];
 
      System.out.println("Transpose of the matrix:");
 
      for (c = 0; c < n; c++)
      {
         for (d = 0; d < m; d++)
               System.out.print(transpose[c][d]+"\t");
 
         System.out.print("\n");
      }
   }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now