C Program to Interchange Elements of First and Last in a Matrix Across Columns Last Updated : 01 Aug, 2022 Comments Improve Suggest changes 1 Likes 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) Create Quiz Comment M mukulsomukesh Follow 1 Improve M mukulsomukesh Follow 1 Improve Article Tags : C Programs C Language C Matrix Programs Explore C BasicsC Language Introduction6 min readIdentifiers in C3 min readKeywords in C2 min readVariables in C4 min readData Types in C3 min readOperators in C8 min readDecision Making in C (if , if..else, Nested if, if-else-if )7 min readLoops in C6 min readFunctions in C5 min readArrays & StringsArrays in C4 min readStrings in C5 min readPointers and StructuresPointers in C7 min readFunction Pointer in C5 min readUnions in C3 min readEnumeration (or enum) in C5 min readStructure Member Alignment, Padding and Data Packing8 min readMemory ManagementMemory Layout of C Programs5 min readDynamic Memory Allocation in C7 min readWhat is Memory Leak? How can we avoid?2 min readFile & Error HandlingFile Handling in C11 min readRead/Write Structure From/to a File in C3 min readError Handling in C8 min readUsing goto for Exception Handling in C4 min readError Handling During File Operations in C5 min readAdvanced ConceptsVariadic Functions in C5 min readSignals in C language5 min readSocket Programming in C8 min read_Generics Keyword in C3 min readMultithreading in C9 min read Like