C# Program to Interchange the Columns of a Matrix

This is a C# Program to interchange any 2 columns of matrix.

Problem Description

This C# Program Interchanges any 2 Columns of a Matrix.

Problem Solution

Here the number of rows, columns and the elements of the matrix are obtained from the user along with the columns that has to be interchanged.

Program/Source Code

Here is source code of the C# Program to Interchange any 2 Columns of a Matrix. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

 * C# Program to Interchange any 2 Columns of a Matrix
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class interchangecol
    int m, n;
    int[,] a;
    public interchangecol(int x, int y)
        m = x;
        n = y;
        a = new int[m, n];
    public void readmatrix()
        Console.WriteLine("Enter the Elements : ");
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                Console.WriteLine("a[{0},{1}] =", i, j);
                a[i, j] = Convert.ToInt32(Console.ReadLine());
    public void printmax()
        Console.WriteLine("Given Matrix : ");
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                Console.Write("{0}\t", a[i, j]);
    public void interchange()
        Console.WriteLine("Enter the Column Number to Interchange : ");
        int i = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Enter the Column Number with which " + 
                          "Interchange is to be Accomplished :");
        int j = Convert.ToInt32(Console.ReadLine());
        for (int k = 0; k < m; k++)
            int temp = a[k, i-1];
            a[k, i-1] = a[k, j-1];
            a[k, j-1] = temp;
    public static void Main()
        int x, y;
        interchangecol obj;
        Console.Write("Enter the Number of Rows :");
        x = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter the Number of Columns :");
        y = Convert.ToInt32(Console.ReadLine());
        obj = new interchangecol(x, y);
Program Explanation

In this C# program, we are reading the number of rows and columns value using ‘x’ and ‘y’ variables respectively.


The readmatrix() is used to enter the coefficient element values of the array using a[i, j] variable. Then printmax() method is used to print the elements in matrix form. The interchange() method is used to interchange the columns.

Using the values of ‘i’ and ‘j’ variables interchange the row and column number. For loop is used to interchange the columns by initializing the value of ‘k’ variable as 0. Check the condition that the value of ‘k’ variable is less than the value of ‘m’ variable.

If the condition is true, then execute the iteration of the loop. The temporary variable ‘temp’ is used to interchange the 2 columns of the matrix. Using printmax() method print the interchanged values of the matrix.

Runtime Test Cases
Enter the Number of Rows : 3
Enter the Number of Columns : 3
Enter the Elements :
Given Matrix is :
1  2  3
4  5  6
7  8  9
Enter the Column Number to Interchange : 2
Enter the Column Number with which Interchange is to be Accomplished : 3
Given Matrix is :
1  3  2
7  9  5
4  6  8

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]

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.