This is a 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.

1. Create a matrix and define its elements.

2. Declare two variables which will store sum of main and opposite diagonal.

3. Now run a single for loop and extract main diagonals elements adding to the first variable and opposite diagonal elements to the second variable.

Here is source code of the C program to finds the sum of the main & opposite diagonal elements of a MxN Matrix. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

`/*`

`* C program to find accept a matrix of order M x N and find`

`* the sum of the main diagonal and off diagonal elements`

`*/`

`#include <stdio.h>`

void main ()

`{`

static int array[10][10];

int i, 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 (i = 0; i < m; ++i)

`{`

for (j = 0; j < n; ++j)

`{`

scanf("%d", &array[i][j]);

`}`

`}`

printf("The given matrix is \n");

for (i = 0; i < m; ++i)

`{`

for (j = 0; j < n; ++j)

`{`

printf(" %d", array[i][j]);

`}`

printf("\n");

`}`

for (i = 0; i < m; ++i)

`{`

sum = sum + array[i][i];

a = a + array[i][m - i - 1];

`}`

printf("\nThe sum of the main diagonal elements is = %d\n", sum);

printf("The sum of the off diagonal elements is = %d\n", a);

`}`

`else`

printf("The given order is not square matrix\n");

`}`

1. Declare a matrix, taking order as input from users and define all its elements.

2. Declare two variable to store sum of each diagonal elements.

3. Run a for loop wherein the main diagonal element is given by index (i, i) where i is the iterator and opposite diagonal element is given by index(i, total_rows(m)-i-1).

4. The two variables are initialized to 0, which are summed up by diagonal elements.

5. Print the sum of diagonal elements.

Enter the order of the matix 2 2 Enter the co-efficients of the matrix 40 30 38 90 The given matrix is 40 30 38 90 The sum of the main diagonal elements is = 130 The sum of the off diagonal elements is = 68

**Sanfoundry Global Education & Learning Series – 1000 C Programs.**

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

**Next Steps:**

- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

**Related Posts:**

- Buy C Books
- Buy Computer Science Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Practice BCA MCQs