Q:

Write a Java Program to Display Transpose Matrix

belongs to collection: Array Programs in Java with Examples

0

Write a Java Program to Display Transpose Matrix

All Answers

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

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

import java.util.Scanner;
public class Javaexcercise
{
    public static void main(String args[])  
    {
    int i, j;
    System.out.println("Enter total rows and columns: ");
    Scanner s = new Scanner(System.in);
    int row = s.nextInt();
    int column = s.nextInt();
    int array[][] = new int[row][column];
    System.out.println("Enter matrix:");
     for(i = 0; i < row; i++)
      {
           for(j = 0; j < column; j++) 
             {
            array[i][j] = s.nextInt();
            System.out.print(" ");
            }
      }
    System.out.println("The above matrix before Transpose is ");
      for(i = 0; i < row; i++)
        {
              for(j = 0; j < column; j++)
            {
              System.out.print(array[i][j]+" ");
            }
            System.out.println(" ");
        }
     System.out.println("The above matrix after Transpose is ");
      for(i = 0; i < column; i++)
        {
              for(j = 0; j < row; j++)
            {
                System.out.print(array[j][i]+" ");
            }
            System.out.println(" ");
        }
    }
}

Result:

Enter total rows and columns: 

2

3

Enter matrix:

1

2

3

4

5

6

The above matrix before Transpose is

1 2 3

4 5 6

The above matrix after Transpose is

1 4

2 5

3 6

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

total answers (1)

Array Programs in Java with Examples

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Java Program to Search Key Elements in an ... >>
<< Write a Java Program to Sort Names in an Alphabeti...