Signup/Sign In
LAST UPDATED: MARCH 5, 2021

Java Program To Interchange any Two Rows and Columns in the given Matrix

In this tutorial, we will learn how to interchange any two rows and columns in the given matrix. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.

Below is the pictorial representation of the interchange of any two rows and columns in the given matrix.

Input: Enter the matrix:

1 2 3

4 5 6

7 8 9

Output: After Interchanging Rows : 2 and 3

1 2 3

7 8 9

4 5 6

After Interchanging Columns: 1 and 3

3 2 1

9 8 7

6 5 4

Program 1: Interchange any Two Rows and Columns

In this program, we will see how to interchange any two rows and columns in the given matrix when the values are user-defined.

Algorithm

  1. Start
  2. Declare variables for rows and columns size.
  3. Ask the user to initialize the number of rows and columns.
  4. Declare a matrix with that size.
  5. Ask the user to initialize the matrix.
  6. Print the Original Matrix.
  7. Use a while loop to check whether the user wants to interchange the rows or column or none.
  8. Use loops to interchange rows and columns respectively.
  9. Ask the user which two rows or columns he/she wants to interchange.
  10. Based on that swap the rows or columns elements.
  11. Display the result.
  12. Stop

Below is the code for the same.

/*Java Program to interchange any two rows and columns in the given matrix*/
import java.util.Scanner;
public class Main 
{
    public static void main(String[] args) 
    {
        int m,n;   //Declare the variables for rows and columns

        //Take input from the user
        Scanner sc = new Scanner(System.in);
        
        System.out.print("Enter number of rows in matrix:");
        m = sc.nextInt();  //Initialize the number of rows

        System.out.print("Enter number of columns in matrix:");
        n = sc.nextInt();  //Initialize the number of columns
 
        int arr[][] = new int[m][n];  //Declare a Matrix
        System.out.println("Enter all the elements of matrix:");
        for (int i = 0; i < m; i++) 
        {
            for (int j = 0; j < n; j++) 
            {
                arr[i][j] = sc.nextInt();
            }
        }

       //Print the original matrix
        System.out.println("The Original Matrix:");
        for (int i = 0; i < m; i++) 
        {
            for (int j = 0; j < n; j++) 
            {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println("");
        }

        //Check whether user want to interchange rows or columns
        while (true) 
        {
            System.out.println("Enter 1 to interchange rows");
            System.out.println("Enter 2 to interchange columns");
            System.out.println("Enter 3 to Exit");
            int n1=sc.nextInt();
            switch (n1) 
            {
                case 1:
                //Enter the rows whose datas you want to interchange 
                System.out.println("Enter the row numbers:");
                int x = sc.nextInt();   
                int y = sc.nextInt();
                int temp=0;
                for(int i = 0; i < m; i++)
                {
                    temp = arr[(x-1)][i];
                    arr[x-1][i] = arr[y-1][i];
                    arr[y-1][i] = temp;
                }
                //Print the matrix after interchanging elements
                System.out.println("Matrix after interchanging rows:"+x +" and "+y);
                for (int i = 0; i < m; i++) 
                {
                    for (int j = 0; j < n; j++) 
                    {
                        System.out.print(arr[i][j] + " ");
                    }
                System.out.println("");
                }
                break;
                case 2:
                //Enter the columns whose datas you want to interchange 
                System.out.println("Enter the column numbers:");
                int p = sc.nextInt();
                int q = sc.nextInt();
                int temp1=0;
                for(int i = 0; i < p; i++)
                {
                    temp1 = arr[i][(p-1)];
                    arr[i][p-1] = arr[i][(q-1)];
                    arr[i][q-1] = temp1;
                }
                //Print the matrix after interchanging elements
                System.out.println("Matrix after interchanging columns:"+p +" and "+q);
                for (int i = 0; i < m; i++) 
                {
                    for (int j = 0; j < n; j++) 
                    {
                        System.out.print(arr[i][j] + " ");
                    }
                System.out.println("");
                }
                break;
          	case 3:
                //Exit from the program
                System.exit(0);
            }
        }
    }
}


Enter number of rows in matrix:3
Enter number of columns in matrix: 3
Enter all the elements of matrix: 1 2 3 4 5 6 7 8 9
The Original Matrix:
1 2 3
4 5 6
7 8 9
Enter 1 to interchange rows
Enter 2 to interchange columns
Enter 3 to Exit
1
Enter the row numbers: 3 2
Matrix after interchanging rows:3 and 2
1 2 3
7 8 9
4 5 6
Enter 1 to interchange rows
Enter 2 to interchange columns
Enter 3 to Exit
2
Enter the column numbers: 1 3
Matrix after interchanging columns:1 and 3
3 2 1
7 8 9
4 5 6
Enter 1 to interchange rows
Enter 2 to interchange columns
Enter 3 to Exit
3

Program 2: To Interchange any Two Rows and Columns

In this program, we will see how to interchange any two rows and columns in the given matrix when the values are pre-defined.

Algorithm

  1. Start
  2. Declare and initialize the matrix.
  3. Print the original matrix.
  4. Enter the columns to be interchanged.
  5. Call the method to interchange the columns.
  6. Swap the numbers to interchange the columns.
  7. Print the matrix after interchanging columns.
  8. Now, call a method to interchange the rows.
  9. Swap the numbers to interchange the rows.
  10. Print the matrix after interchanging rows.
  11. Stop.

Below is the code for the same.

/*Java Program to interchange any two rows and columns*/
import java.io.*; 
  
public class Main 
{
    //To Print the arr
    public static void printMatrix(int[][] arr) 
    { 
        for (int i = 0; i < arr.length; i++) { 
            for (int j = 0; j < arr[0].length; j++) 
                System.out.print(arr[i][j] + " "); 
            System.out.println(); 
        } 
    } 
    
    //To Interchange Columns  
    public static void interchangeColumns(int[][] arr, 
                                             int K, int L) 
    { 
        for (int i = 0; i < arr.length; i++) { 
  
            // Swap two numbers 
            int temp = arr[i][K - 1]; 
            arr[i][K - 1] = arr[i][L - 1]; 
            arr[i][L - 1] = temp; 
        } 
        System.out.println("After Interchanging Column "+ K + " and "+ L+ " :");
        // Print arr 
        printMatrix(arr); 
    } 
    
    //To Interchange Rows  
    public static void interchangeRows(int[][] arr, int x, int y) 
    { 
        for (int i = 0; i < arr[0].length; i++) { 
            
            // Swap two numbers 
            int temp = arr[x - 1][i]; 
            arr[x - 1][i] = arr[y - 1][i]; 
            arr[y - 1][i] = temp; 
        } 
        System.out.println("After Interchanging Row "+ x + " and "+ y + " :");
        // Print arr 
        printMatrix(arr); 
    } 
    
    
      
    // Driver code 
    public static void main(String args[]) throws IOException 
    { 
        int arr[][] 
            = { { 2, 9, 8 }, { 7, 6, 4 }, { 3, 9, 2 } };  //Matrix Declaration and Initialization
            
        //Print Original arr    
        System.out.println("Original arr: ");
        printMatrix(arr); 
        
        // calling to exchange Columns
        int K = 1, L = 2; 
        interchangeColumns(arr, K, L); 
        
        // calling to exchange Rows
         int K1 = 1, L1 = 2; 
        interchangeRows(arr, K1, L1); 
          
        
    } 
} 


Original arr:
2 9 8
7 6 4
3 9 2
After Interchanging Column 1 and 2 :
9 2 8
6 7 4
9 3 2
After Interchanging Row 1 and 2 :
6 7 4
9 2 8
9 3 2



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.