Skip to content
Related Articles

Related Articles

Java Program to Interchange Elements of First and Last in a Matrix Across Columns
  • Last Updated : 17 Mar, 2021

For a given 4 x 4 matrix, the task is to interchange the elements of the first and last columns and then return the resultant matrix.

Examples :

Input 1 :   1  1  5  0
            2  3  7  2
            8  9  1  3
            6  7  8  2

Output 1 :  0  1  5  1
            2  3  7  2
            3  9  1  8
            2  7  8  6
 
Input 2 :   7   8  9  10
           11  13  14  1
           15   7  12 22
           11  21  30  1

Output 2 : 10  8   9   7
           1  13  14  11
           22  7  12  15
           1  21  30  11

Approach:

To get the required output, we need to swap the elements of the first and last column of the stated matrix.

Example

Java




// Java Program to Interchange Elements of the
// First and Last Column in a Matrix
  
// Importing input output classes
import java.io.*;
  
class GFG {
  
    // Declare static variable and initialize to
    // order of the matrix
    static int N = 3;
  
    // Method 1
    // To swap first and last column in a matrix
    static void Swap_First_Last(int mat[][])
    {
        int cls = N;
  
        // Interchanging of elements between the
        // first and last columns
        for (int j = 0; j < N; j++) {
            int temp = mat[j][0];
            mat[j][0] = mat[j][N - 1];
            mat[j][N - 1] = temp;
        }
    }
  
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Creating 2D integer element matrix
        // Custom input matrix
        int mat[][]
            = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
  
        // Outer loop for rows
        for (int j = 0; j < N; j++) {
  
            // Inner loop for columns
            for (int k = 0; k < N; k++) {
  
                // Print the input matrix
                System.out.print(mat[j][k] + " ");
            }
            // Operations over a row is computed so new line
            System.out.println();
        }
  
        System.out.println("Swapped Matrix as follows : ");
  
        // Now, calling the (Method1) to interchange
        // first and last columns in above matrix
        Swap_First_Last(mat);
  
        // Now simply print the updated matrix
        // Swapped matrix using nested for loops
  
        // Outer loop for rows
        for (int j = 0; j < N; j++) {
  
            // Inner loop for columns
            for (int k = 0; k < N; k++)
  
                // Print the swapped matrix
                System.out.print(mat[j][k] + " ");
  
            // Operations over a row is computed so new line
            System.out.println();
        }
    }
}
Output
1 2 3 
4 5 6 
7 8 9 
Swapped Matrix as follows : 
3 2 1 
6 5 4 
9 8 7 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :