
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C program to interchange the diagonal elements in given matrix
Problem
We need to write a code to interchange the main diagonal elements with the secondary diagonal elements. The size of the matrix is given at runtime.
If the size of matrix m and n values are not equal, then it prints that the given matrix is not a square.
Only a square matrix can interchange the main diagonal elements and can interchange with the secondary diagonal elements.
Solution
The solution to write a C program to interchange the diagonal elements in given matrix is as follows −
The logic to interchange the diagonal elements is explained below −
for (i=0;i<m;++i){ a = ma[i][i]; ma[i][i] = ma[i][m-i-1]; ma[i][m-i-1] = a; }
Example
Following is the C program to interchange the diagonal elements in given matrix −
#include<stdio.h> main (){ int i,j,m,n,a; static int ma[10][10]; printf ("Enter the order of the matrix m and n
"); scanf ("%dx%d",&m,&n); if (m==n){ printf ("Enter the co-efficients of the matrix
"); for (i=0;i<m;++i){ for (j=0;j<n;++j){ scanf ("%d",&ma[i][j]); } } printf ("The given matrix is
"); for (i=0;i<m;++i){ for (j=0;j<n;++j){ printf (" %d",ma[i][j]); } printf ("
"); } for (i=0;i<m;++i){ a = ma[i][i]; ma[i][i] = ma[i][m-i-1]; ma[i][m-i-1] = a; } printf ("Matrix after changing the
"); printf ("Main & secondary diagonal
"); for (i=0;i<m;++i){ for (j=0;j<n;++j){ printf (" %d",ma[i][j]); } printf ("
"); } } else printf ("The given order is not square matrix
"); }
Output
When the above program is executed, it produces the following result −
Run 1: Enter the order of the matrix m and n 3x3 Enter the co-efficient of the matrix 1 2 3 4 5 6 7 8 9 The given matrix is 1 2 3 4 5 6 7 8 9 Matrix after changing the Main & secondary diagonal 3 2 1 4 5 6 9 8 7 Run 2: Enter the order of the matrix m and n 4x3 The given order is not square matrix
- Related Articles
- Program to convert given Matrix to a Diagonal Matrix in C++
- Program to Interchange Diagonals of Matrix in C program
- Program to check diagonal matrix and scalar matrix in C++
- Program to sort each diagonal elements in ascending order of a matrix in C++
- Swift Program to Interchange Elements of First and Last Columns of Matrix
- Java Program to Interchange Elements of First and Last in a Matrix Across Rows
- Java Program to Interchange Elements of First and Last in a Matrix Across Columns
- Golang Program To Interchange Elements Of First And Last In A Matrix Across Columns
- Golang Program To Interchange Elements Of First And Last In A Matrix Across Rows
- Swift Program to Interchange Elements of First and Last Rows of a Matrix
- Swift Program to Interchange the Diagonals of a Matrix
- Python Program to Remove First Diagonal Elements from a Square Matrix
- Print numbers in matrix diagonal pattern in C Program.
- Swift Program to print the left diagonal matrix
- Swift Program to print the right diagonal matrix

Advertisements