Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Monday, 6 November 2017

Java Program to Display Transpose of a Matrix

import java.util.Scanner;

public class Transpose
{
   public static void main(String args[])
   {
       int i, j n,m;
       int arr[][] = new int[10][10];
       int Tran[][] = new int[10][10];
       Scanner sc = new Scanner(System.in);
   
       System.out.print("Enter no of rows and columns : ");
       n=sc.nextInt();

       m=sc.nextint();      

       for(i=0; i<n; i++)
       {
           for(j=0; j<m; j++)
           {
               arr[i][j] = sc.nextInt();
           }
       }
   
       System.out.print("Transposing Array...\n");
       for(i=0; i<n; i++)
       {
           for(j=0; j<m; j++)
           {
               Tran[i][j] = arr[j][i];
           }
       }
   
       System.out.print("Transpose of the Matrix is :\n");
       for(i=0; i<n; i++)
       {
           for(j=0; j<m; j++)
           {
               System.out.print(Tran[i][j]+ " ");
           }
           System.out.println();
       }
   }
}

No comments:

Post a Comment