C# Program to Find Transpose of a Matrix

This is a C# Program to generate the transpose of a given matrix.

Problem Description

This C# Program Generates the Transpose of a Given Matrix.

Problem Solution

The transpose of a given matrix is formed by interchanging the rows and columns of a matrix.

Program/Source Code

Here is source code of the C# Program to Generate the Transpose of a Given Matrix. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Generate the Transpose of a Given Matrix
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Program
{
    class Program
    {
        public static void Main(string[] args)
        {
            int m, n, i, j;
            Console.Write("Enter the Order of the Matrix : ");
            m = Convert.ToInt16(Console.ReadLine());
            n = Convert.ToInt16(Console.ReadLine());
            int[,] A = new int[10, 10];
            Console.Write("\nEnter The Matrix Elements : ");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    A[i, j] = Convert.ToInt16(Console.ReadLine());
                }
            }
            Console.Clear();
            Console.WriteLine("\nMatrix A : ");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    Console.Write(A[i, j] + "\t");
 
                }
                Console.WriteLine();
            }
            Console.WriteLine("Transpose Matrix : ");
 
           for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    Console.Write(A[j, i] + "\t");
 
                }
                Console.WriteLine();
            }
            Console.Read();
        }
    }
}
Program Explanation

In this C# Program, we are reading the order of the matrix using ‘m’ and ‘n’ variables respectively. The transpose of a given matrix is formed by interchanging the rows and columns of a matrix. Using for loop we are entering the coefficient values of an element to the array variable A[i,j].

advertisement
advertisement

The transpose method is used to interchange the rows and columns. Using for loop initialize the value of ‘i’ variable as 0. Check the condition that the value of ‘i’ variable is less than the value of ‘m’ variable. If the condition is true then execute the iteration of the loop. Inside the loop another for loop is used because it’s a two dimensional matrix.

In that for loop initialize the value of ‘i’ variable as 0 and check the condition that the value of ‘i’ variable is less than the value of ‘n’ variable. If the condition is true then execute the iteration of the loop. Print the transpose value in the A[] array variable with base index ‘j’ and ‘i’ variables respectively.

Runtime Test Cases
 
Enter the Order of the Matrix : 2  2
Enter the Matrix Elements :
1 2
3 4
Matrix A :
1 2
3 4
Transpose Matrix :
1 3
2 4

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.