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

1. Create a matrix of some order and define its elements using for loop.

2. Now run a for loop till the number of rows times.

3. The element at first row and first column is exchanged with element of first row and last column.

4. Change element at second row and second column is exchanged with element of second row and last second column.

5. Continue with the pattern, at the end we will get diagonals exchanged.

Here is source code of the C program to accept a matrix of order MxN & interchange the diagonals. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

`/*`

`* 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[10][10];

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

`{`

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

`{`

scanf("%dx%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)

`{`

a = array[i][i];

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

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

`}`

printf("The matrix after changing the \n");

printf("main diagonal & secondary diagonal\n");

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

`{`

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

`{`

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

`}`

printf("\n");

`}`

`}`

`else`

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

`}`

1. Declare a 2D array of some capacity.

2. Take its order as input from users and define its elements according to the order and using for loop.

3. Now print the matrix.

4. Run a for loop the number of rows in the matrix times.

5. The row index of the both the elements which are meant to interchange remains same for same row. But the element of first row gets exchanged with last row, then second column element gets interchanged with second last column of the matrix and so on.

6. After the completion of for loop, print the matrix with diagonal elements being interchanged

Enetr the order of the matix 2 2 Enter the co-efficients of the matrix 25 30 78 43 The given matrix is 25 30 78 43 The matrix after changing the main diagonal & secondary diagonal 30 25 43 78

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

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

**If you find any mistake above, kindly email to [email protected]**

**Related Posts:**

- Practice BCA MCQs
- Check C Books
- Apply for C Internship
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos