 # C Programming Code Examples

## C > Arrays and Matrices Code Examples

### Program to Accept a Matrix of Order MxN & Interchange the Diagonals

``` Program to Accept a Matrix of Order MxN & Interchange the Diagonals This C Program accepts matrix of order MxN & interchange the diagonals. This program first accepts the matrix. Then exchange diagonals of the matrix. /* * C program to accept a matrix of order M x N and store its elements * and interchange the main diagonal elements of the matrix * with that of the secondary diagonal elements */ #include <stdio.h> void main () { static int array; int y, x, m, n, a; printf("Enter the order of the matix \n"); scanf("%d %d", &m, &n); if (m == n) { printf("Enter the co-efficients of the matrix\n"); for (y = 0; y < m; ++y) { for (x = 0; x < n; ++x) { scanf("%dx%d", &array[y][x]); } } printf("The given matrix is \n"); for (y = 0; y < m; ++y) { for (x = 0; x < n; ++x) { printf(" %d", array[y][x]); } printf("\n"); } for (y = 0; y < m; ++y) { a = array[y][y]; array[y][y] = array[y][m - y - 1]; array[y][m - y - 1] = a; } printf("The matrix after changing the \n"); printf("main diagonal & secondary diagonal\n"); for (y = 0; y < m; ++y) { for (x = 0; x < n; ++x) { printf(" %d", array[y][x]); } printf("\n"); } } else printf("The given order is not square matrix\n"); } ``` 