In this tutorial you will learn about the C Program to Interchange Diagonals of a Matrix and its application with practical example.
C Program to Interchange Diagonals of a Matrix
In this tutorial, we will learn to create a C program that will Interchange Diagonals of a Matrix using C programming.
Prerequisites
Before starting with the tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C programming.
Algorithm:-
Below is an example of the matrix before interchanging of diagonals figure1
Figure1
Then After interchanging of diagonals of the matrix is given below.
Figure 2
With the help of this program, we can Interchange the Diagonals of a Matrix.
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the variables for the program. 2. Taking the input size of matrix. 3. Taking the input elements of matrix. 4. interchanging the diagonal elements of the matrix<strong>.</strong> 5. Printing the result<strong> Matrix</strong>. 6. End Program. |
Program to Interchange Diagonals of a Matrix:-
In this tutorial, we will interchange Diagonal Elements in a Matrix. First, we will take the size of the matrix from the user, and then we will take the input elements of the matrix. Now we will swap the diagonal elements in the matrix. At last, we will print the result matrix to the user.
Program:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
/* C program to interchange Diagonals of a Matrix */ #include<stdio.h> int main() { //Declaring the required variables for the program. int i, j, x, y, arr[10][10], temp; //Taking the size of the matrix. printf("\n Please Enter Number of Rows and Columns : "); scanf("%d %d", &i, &j); //Taking the elements of the matrix. printf("\n Please Enter the Matrix Elements \n"); for(x = 0; x < i; x++) { for(y = 0;y < j;y++) { scanf("%d", &arr[x][y]); } } //Interchanging the elements of the diagonals of the matrix if(x == y) { for(x = 0; x < i; x++) { temp = arr[x][x]; arr[x][x] = arr[x][i - x - 1]; arr[x][i - x - 1] = temp; } //Printing the matrix after changing the elements of the diagonals. printf("\n Matrix Elements after Interchanging Diagonals are: \n"); for(x = 0; x < j; x++) { for(y = 0; y < i; y++) { printf("%d \t ", arr[x][y]); } printf("\n"); } } else { printf("\n The Matrix that you entered is Not a Square matrix" ); } return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- a[10][10]= it will hold the integer value.
- x = it will hold the integer value for the loop.
- y = it will hold the integer value for the loop.
- i= it will hold the input integer value for rows.
- j= it will hold the input integer value for columns.
Taking the size and the elements of the matrix from the user.
Interchanging the elements of the diagonals of the matrix.
Printing the matrix after interchanging the elements of the Matrix.