C Programming Code Examples C > Arrays and Matrices Code Examples C program to interchange diagonals of a matrix C program to interchange diagonals of a matrix Write a C program to read elements in a matrix and interchange elements of primary(major) diagonal with secondary(minor) diagonal. C program for interchanging diagonals of a matrix. #include <stdio.h> #define maxrows 3 #define maxcols 5 int main() { int A[maxrows][maxcols]; int row, col, size, temp; /* Input elements in matrix from user */ printf("Enter elements in matrix of size %dx%d: \n", maxrows, maxcols); for(row=0; row<maxrows; row++) { for(col=0; col<maxcols; col++) { scanf("%d", &A[row][col]); } } size = (maxrows < maxcols) ? maxrows : maxcols; /* Interchange diagonal of the matrix */ for(row=0; row<size; row++) { col = row; temp = A[row][col]; A[row][col] = A[row][(size-col) - 1]; A[row][(size-col) - 1] = temp; } /* Print the interchanged diagonals matrix */ printf("\nMatrix after diagonals interchanged: \n"); for(row=0; row<maxrows; row++) { for(col=0; col<maxcols; col++) { printf("%d ", A[row][col]); } printf("\n"); } return 0; }