C# Program to perform Matrix Transpose

You are Here:

Perform Matrix Transpose

In the following example, we will transpose the given matrix (two-dimensional array).

Example

C# Compiler
using System; namespace myApp { class Program { static void Main(string[] args) { int i, j, n; int[,] arr = { {1, 1, 1}, {2, 2, 2}, {3, 3, 3} }; Console.WriteLine("Matrix (3 x 3):"); for(i=0; i<3; i++) { for(j=0; j<3; j++) Console.Write("{0} ", arr[i, j]); Console.WriteLine(); } for(i=0; i<3; i++) { for(j=i+1; j<3; j++) { n = arr[i, j]; arr[i, j] = arr[j, i]; arr[j, i] = n; } } Console.WriteLine("\nMatrix Transpose:"); for(i=0; i<3; i++) { for(j=0; j<3; j++) Console.Write("{0} ", arr[i, j]); Console.WriteLine(); } } } }

In the following example, we will get the values for (3 x 3) Matrix from the user and display the transpose matrix.

Example

C# Compiler
using System; namespace myApp { class Program { static void Main(string[] args) { int i, j, n; int[,] arr = new int[3, 3]; Console.WriteLine("Enter Matrix (3 x 3):"); for(i=0; i<3; i++) { for(j=0; j<3; j++) { Console.Write("a[{0}, {1}] = ", i, j); arr[i, j] = Convert.ToInt32(Console.ReadLine()); } } for(i=0; i<3; i++) { for(j=i+1; j<3; j++) { n = arr[i, j]; arr[i, j] = arr[j, i]; arr[j, i] = n; } } Console.WriteLine("\nMatrix Transpose:"); for(i=0; i<3; i++) { for(j=0; j<3; j++) Console.Write(arr[i, j] +" "); Console.WriteLine(); } } } }

Reminder

Hi Developers, we almost covered 90% of String functions and Interview Question on C# with examples for quick and easy learning.

We are working to cover every Single Concept in C#.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author