 # C Programming Code Examples

## C > Arrays and Matrices Code Examples

### Program to do the Sum of the Main & Opposite Diagonal Elements of a MxN Matrix

``` Program to do the Sum of the Main & Opposite Diagonal Elements of a MxN Matrix This C Program finds the sum of the main & opposite diagonal elements of a MxN Matrix. The program accepts an MxN matrix. Then adds main diagonal of matrix as well as the opposite diagonal of the matrix. #include <stdio.h> void main () { static int array; int x, j, m, n, a = 0, sum = 0; printf("Enetr the order of the matix \n"); scanf("%d %d", &m, &n); if (m == n ) { printf("Enter the co-efficients of the matrix\n"); for (x = 0; x < m; ++x) { for (j = 0; j < n; ++j) { scanf("%d", &array[x][j]); } } printf("The given matrix is \n"); for (x = 0; x < m; ++x) { for (j = 0; j < n; ++j) { printf(" %d", array[x][j]); } printf("\n"); } for (x = 0; x < m; ++x) { sum = sum + array[x][x]; a = a + array[x][m - x - 1]; } printf("\nThe sum of the main diagonal elements is = %d\n", sum); printf("The sum of the off diagonal elemets is = %d\n", a); } else printf("The given order is not square matrix\n"); } ``` 