**Write a C program to swap major diagonal and minor diagonal of a square matrix.**

WAP to interchange diagonal elements of a square matrix.

WAP to interchange diagonal elements of a square matrix.

#### Required Knowledge

This program takes a square matrix as input from user and **swaps the element of major and minor diagonals**.

For Example:

Input Matrix 1 2 3 4 5 6 7 8 9 Output Matrix 3 2 1 4 5 6 9 8 7

**Algorithm to swap major and minor diagonal elements of a square matrix**

*Let inputMatrix is a square matrix of row and column dimension N.*

- For every row, we will
**swap the elements of major and minor diagonals**. - In any row R, the major diagonal element will be at inputMatrix[R][R] and minor diagonal element will be at inputMatrix[R][COLS-R-1] where COLS is the total number of columns in square matrix inputMatrix.

## C program to sort an array in increasing order using bubble sort

/* * C Program to interchange Major and Minor diagonals of a Matrix */ #include <stdio.h> #include <conio.h> int main(){ int rows, cols, row, col, temp; int matrix[50][50]; printf("Enter Rows and Columns of Square Matrix\n"); scanf("%d %d", &rows, &cols); printf("Enter Matrix of size %dX%d\n", rows, cols); for(row = 0; row < rows; row++){ for(col = 0; col < cols; col++){ scanf("%d", &matrix[row][col]); } } /* Interchange Major and Minor diagonals of Matrix */ for(row = 0; row < rows; row++) { col = row; temp = matrix[row][col]; matrix[row][col] = matrix[row][(cols-col)-1]; matrix[row][(cols-col)-1] = temp; } printf("Matrix After Swapping Diagonals\n"); for(row = 0; row < rows; row++){ for(col = 0; col < cols; col++){ printf("%d ", matrix[row][col]); } printf("\n"); } getch(); return 0; }Output

Enter Rows and Columns of Square Matrix 3 3 Enter Matrix of size 3X3 1 2 3 4 5 6 7 8 9 Matrix After Swapping Diagonals 3 2 1 4 5 6 9 8 7

**Related Topics**