codescracker


java

Java Program to Transpose Matrix



« Previous Program Next Program »


Transpose Matrix

To transpose any matrix in Java Programming, first you have to ask to the user to enter the matrix elements. Now, to transpose any matrix, you have to replace the row elements by the column elements and vice-versa.

Java Programming Code to Transpose Matrix

Following Java Program ask to the user to enter the 3*3 array element to transpose and display the transpose of the Matrix on the screen:

/* Java Program Example - Transpose Matrix */
		
import java.util.Scanner;

public class JavaProgram
{
   public static void main(String args[])
   {
       int i, j;
       int arr[][] = new int[3][3];
       int arrt[][] = new int[3][3];
       Scanner scan = new Scanner(System.in);
	   
       System.out.print("Enter 3*3 Array Elements : ");
       for(i=0; i<3; i++)
       {
           for(j=0; j<3; j++)
           {
               arr[i][j] = scan.nextInt();
           }
       }
	   
       System.out.print("Transposing Array...\n");
       for(i=0; i<3; i++)
       {
           for(j=0; j<3; j++)
           {
               arrt[i][j] = arr[j][i];
           }
       }
	   
       System.out.print("Transpose of the Matrix is :\n");
       for(i=0; i<3; i++)
       {
           for(j=0; j<3; j++)
           {
               System.out.print(arrt[i][j]+ " ");
           }
           System.out.println();
       }
   }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program transpose matrix

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test