Basic Programming C# Examples

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below:

using System;

namespace MatrixTranspose
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, enter the number of rows and columns in the matrix: ");
            int rows = int.Parse(Console.ReadLine());
            int columns = int.Parse(Console.ReadLine());

            int[,] matrix = new int[rows, columns];
            int[,] transpose = new int[columns, rows];

            Console.WriteLine("Enter the elements of the matrix: ");
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    matrix[i, j] = int.Parse(Console.ReadLine());
                }
            }

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    transpose[j, i] = matrix[i, j];
                }
            }

            Console.WriteLine("Okay, the transpose of the matrix is: ");
            for (int i = 0; i < columns; i++)
            {
                for (int j = 0; j < rows; j++)
                {
                    Console.Write(transpose[i, j] + " ");
                }
                Console.WriteLine();
            }
        }
    }
}

Output:

Hello Codeauri Family, enter the number of rows and columns in the matrix:
2
2
Enter the elements of the matrix:
4
1
2
5
Okay, the transpose of the matrix is:
4 2
1 5

Pro-Tips💡

Here are the steps by steps execution of above program:

  1. The first line of the program using System; brings in the System namespace which contains the Console class used for input and output in the program.
  2. In the Main method, the program first prompts the user to enter the number of rows and columns in the matrix. It stores the values in the variables “rows” and “columns”.
  3. Then, it creates two two-dimensional arrays: “matrix” and “transpose”. “matrix” is initialized with the given number of rows and columns and “transpose” is initialized with the number of columns and rows (since the number of rows and columns in the transpose of a matrix are swapped).
  4. Next, the program prompts the user to enter the elements of the matrix and stores them in the “matrix” array.
  5. In the following step, the program calculates the transpose of the matrix. It does this by switching the values of the rows and columns in the “matrix” array and storing the result in the “transpose” array.
  6. Finally, the program outputs the transpose of the matrix by printing the elements of the “transpose” array.
  7. The program ends with the closing brace of the Main method.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X