C Program to Interchange Elements of First and Last in a Matrix Across Columns Last Updated : 01 Aug, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Here, we will see how to interchange the elements of the first and last element/entries in a matrix across columns; with an approach of swapping. Input: 9 7 5 2 3 4 5 2 6 Output: 5 7 9 4 3 2 6 2 5Approach: Swapping The approach is very simple for this program, we can simply swap the elements of the first and last columns of the matrix in order to get the desired matrix as output.Example: C // C program to swap the element of first and last column of // the matrix and display the result #include <stdio.h> #define n 3 // macros void interchangeFirstLast(int mat[][n]) { // swap the elements between first and last columns for (int i = 0; i < n; i++) { int t = mat[i][0]; mat[i][0] = mat[i][n - 1]; mat[i][n - 1] = t; } } // Driver code int main() { // input matrix int mat[n][n] = { { 2, 4, 6 }, { 8, 2, 3 }, { 1, 9, 4 } }; // print input matrix printf("Input Matrix: \n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%d ", mat[i][j]); } printf("\n"); } // call interchangeFirstLast(mat) function. // This function swap the element of first and last // columns. interchangeFirstLast(mat); // print output matrix printf("Output Matrix: \n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%d ", mat[i][j]); } printf("\n"); } } OutputInput Matrix: 2 4 6 8 2 3 1 9 4 Output Matrix: 6 4 2 3 2 8 4 9 1 Time Complexity: O(n)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article JavaScript Program to Interchange Elements of First & Last in a Matrix Across Columns M mukulsomukesh Follow Improve Article Tags : C Programs C Language C Matrix Programs Similar Reads C Program to Interchange Elements of First and Last in a Matrix Across Rows Here, we will see how to interchange the elements of first and last in a matrix across rows. Below are the examples: Input: 6 3 1 4 5 2 2 4 9Output: 2 4 9 4 5 2 6 3 1 Input: 1 3 5 4 5 2 2 2 4 Output: 2 2 4 4 5 2 1 3 5 Approach: The approach is very simple, swap the elements of the first and last row 2 min read C Program to Interchange Two Random Rows in a Matrix In this article, we will write a C program to interchange two random rows in a matrix. Below are the inputs that will be taken from the user: The number of rows & columns in the matrixThe elements in the matrixThe rows that will be interchanged Examples: Input: rows = 3, columns = 3 arr[i, j] = 2 min read C Program for Turn an image by 90 degree Given an image, how will you turn it by 90 degrees? A vague question. Minimize the browser and try your solution before going further. An image can be treated as 2D matrix which can be stored in a buffer. We are provided with matrix dimensions and it's base address. How can we turn it? For example s 4 min read Efficient method to store a Lower Triangular Matrix using Column-major mapping Given a lower triangular matrix Mat[][], the task is to store the matrix using column-major mapping. Lower Triangular Matrix: A Lower Triangular Matrix is a square matrix in which the lower triangular part of a matrix consists of non-zero elements and the upper triangular part consists of 0s. The Lo 10 min read JavaScript Program to Interchange Elements of First & Last in a Matrix Across Columns We are given a matrix and we have to interchange the elements of the first and last columns with each other in a matrix. Example: Input 1 : 1 1 5 0 2 3 7 2 8 9 1 3 6 7 8 2Output 1 : 0 1 5 1 2 3 7 2 3 9 1 8 2 7 8 6Table of Content Using the swapping logicUsing forEach methodUsing the swapping logicIn 3 min read Program to Interchange or Swap Columns in a Matrix Given a matrix having m rows and n columns, the task is to write a program to interchange any two Columns(i.e. column no. N and M given in the input) in the given matrix. Example: Input : N = 1, M = 2, mat[][] = {{2, 1, 4}, {1, 2, 3}, {3, 6, 2}}Output: mat[][] = {{1, 2, 4}, {2, 1, 3}, {6, 3, 2}} Inp 6 min read Like