Transpose of matrix



CODE:
import java.util.*;
class Transpose
{
     public static void main(String args[])
     {
           int m,n,i,j;
           Scanner sc = new Scanner(System.in);
           System.out.print("Enter no of rows & columns :");
           m=sc.nextInt();
           n=sc.nextInt();
           int a[][]=new int[m][n];
           int b[][]=new int[n][m];
           System.out.println("Matrix A");
           for(i=0;i<=m-1;i++)
           {
                for(j=0;j<=n-1;j++)
                {
                     System.out.print("enter a no:");
                     a[i][j]=sc.nextInt();
                }
           }
           for(i=0;i<=m-1;i++)
           {
                for(j=0;j<=n-1;j++)
                {
                     b[j][i]=a[i][j];
                }
           }
           System.out.println("Transpose Matrix");
           for(i=0;i<=m-1;i++)
           {
                for(j=0;j<=n-1;j++)
                {
                     System.out.print(b[i][j]+"/t");
                }
                System.out.println();
           }
     }
}         

OUTPUT:    

      



No comments :

Post a Comment