w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: Transpose of a Matrix

C# Sharp Array: Exercise-22 with Solution

Write a program in C# Sharp to find transpose of a given matrix.

C# Sharp: Transpose of a Matrix

Sample Solution:-

C# Sharp Code:

using System;  
public class Exercise22 
{  
    public static void Main()
      {
        int i,j,r,c;
      	int[,] arr1 = new int[50,50];
		int[,] brr1 = new int[50,50];

  
       Console.Write("\n\nTranspose of a Matrix :\n");
       Console.Write("---------------------------\n"); 
   
  Console.Write("\nInput the number of rows and columns of the first matrix :\n");	   
  Console.Write("Rows : ");
  r = Convert.ToInt32(Console.ReadLine());
  Console.Write("Columns : ");  
  c = Convert.ToInt32(Console.ReadLine());	   
	   

       Console.Write("Input elements in the matrix :\n");
       for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
	           Console.Write("element - [{0}],[{1}] : ",i,j);
			   arr1[i,j]=Convert.ToInt32(Console.ReadLine());	 
            }
        } 

 	 Console.Write("\nThe First matrix is :\n");
  		for(i=0;i<r;i++)
    		{
      		Console.Write("\n");
      		for(j=0;j<c;j++)
          	Console.Write("{0}\t",arr1[i,j]);
    		}
  
  for(i=0;i<r;i++)
      {
      for(j=0;j<c;j++)
           {

                   brr1[j,i]=arr1[i,j];
            }
       }

      Console.Write("\n\nThe Transpose of a matrix is : ");
      for(i=0;i<c;i++)
	  {
      Console.Write("\n");
      for(j=0;j<r;j++)
	  {
           Console.Write("{0}\t",brr1[i,j]);
      }
  }
      Console.Write("\n\n");
	  }
}

Sample Output:

Transpose of a Matrix :                                                                                       
---------------------------                                                                                   
                                                                                                              
Input the number of rows and columns of the first matrix :                                                    
Rows : 2                                                                                                      
Columns : 2                                                                                                   
Input elements in the matrix :                                                                                
element - [0],[0] : 1                                                                                         
element - [0],[1] : 2                                                                                         
element - [1],[0] : 3                                                                                         
element - [1],[1] : 4                                                                                         
                                                                                                              
The First matrix is :                                                                                         
                                                                                                              
1       2                                                                                                     
3       4                                                                                                     
                                                                                                              
The Transpose of a matrix is :                                                                                
1       3                                                                                                     
2       4

Flowchart:

Flowchart: Transpose of a Matrix

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp for multiplication of two square Matrices.
Next: Write a program in C# Sharp to find sum of right diagonals of a matrix.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming