Program to swap upper diagonal elements with lower diagonal elements of matrix.
Given a square matrix, swap upper diagonal elements of matrix with lower diagonal elements of matrix.
Examples :
Input: 2 3 5 6
4 5 7 9
8 6 4 9
1 3 5 6
Output: 2 4 8 1
3 5 6 3
5 7 4 5
6 9 9 6
Input: 1 2 3
4 5 6
7 8 9
Output: 1 4 7
2 5 8
3 6 9
Below is the implementation of above idea :
C++
// CPP Program to implement matrix // for swapping the upper diagonal // elements with lower diagonal // elements of matrix. #include <bits/stdc++.h> #define n 4 using namespace std; // Function to swap the diagonal // elements in a matrix. void swapUpperToLower(int arr[n][n]) { // Loop for swap the elements of matrix. for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int temp = arr[i][j]; arr[i][j] = arr[j][i]; arr[j][i] = temp; } } // Loop for print the matrix elements. for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << arr[i][j] << " "; cout << endl; } } // Driver function to run the program int main() { int arr[n][n] = { { 2, 3, 5, 6 }, { 4, 5, 7, 9 }, { 8, 6, 4, 9 }, { 1, 3, 5, 6 } }; // Function call swapUpperToLower(arr); return 0; } } |
chevron_right
filter_none
Java
// java Program to implement matrix // for swapping the upper diagonal // elements with lower diagonal // elements of matrix. import java.io.*; class GFG { static int n = 4; // Function to swap the diagonal // elements in a matrix. static void swapUpperToLower(int arr[][]) { // Loop for swap the elements of matrix. for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int temp = arr[i][j]; arr[i][j] = arr[j][i]; arr[j][i] = temp; } } // Loop for print the matrix elements. for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) System.out.print( arr[i][j] +" "); System.out.println(); } } // Driver code public static void main (String[] args) { int arr[][] = { { 2, 3, 5, 6 }, { 4, 5, 7, 9 }, { 8, 6, 4, 9 }, { 1, 3, 5, 6 } }; // Function call swapUpperToLower(arr); } } // This code is contributed by vt_m. |
chevron_right
filter_none
Python 3
# Python Program to implement matrix # for swapping the upper diagonal # elements with lower diagonal # elements of matrix. # Function to swap the diagonal # elements in a matrix. def swapUpperToLower(arr): n = 4; # Loop for swap the elements # of matrix. for i in range(0, n): for j in range(i + 1, n): temp = arr[i][j]; arr[i][j] = arr[j][i]; arr[j][i] = temp; # Loop for print the matrix elements. for i in range(0, n): for j in range(0, n): print(arr[i][j], end = " "); print(" "); # Driver Code arr = [[2, 3, 5, 6 ],[ 4, 5, 7, 9 ], [8, 6, 4, 9 ],[ 1, 3, 5, 6 ]]; # Function call swapUpperToLower(arr); # This code is contributed # by Shivi_Aggarwal |
chevron_right
filter_none
C#
// C# Program to implement matrix // for swapping the upper diagonal // elements with lower diagonal // elements of matrix. using System; class GFG { static int n = 4; // Function to swap the diagonal // elements in a matrix. static void swapUpperToLower(int [,]arr) { // Loop for swap the elements of matrix. for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int temp = arr[i, j]; arr[i, j] = arr[j, i]; arr[j, i] = temp; } } // Loop for print the matrix elements. for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) Console.Write(arr[i, j] +" "); Console.WriteLine(); } } // Driver code public static void Main () { int [,]arr = {{ 2, 3, 5, 6 }, { 4, 5, 7, 9 }, { 8, 6, 4, 9 }, { 1, 3, 5, 6 }}; // Function call swapUpperToLower(arr); } } // This code is contributed by vt_m. |
chevron_right
filter_none
PHP
<?php // PHP Program to implement matrix // for swapping the upper diagonal // elements with lower diagonal // elements of matrix. $n = 4; // Function to swap the diagonal // elements in a matrix. function swapUpperToLower($arr) { global $n; // Loop for swap the elements of matrix. for ($i = 0; $i < $n; $i++) { for ($j = $i + 1; $j < $n; $j++) { $temp = $arr[$i][$j]; $arr[$i][$j] = $arr[$j][$i]; $arr[$j][$i] = $temp; } } // Loop for print the matrix elements. for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) echo($arr[$i][$j] . " "); echo("\n"); } } // Driver Code $arr = array(array(2, 3, 5, 6), array(4, 5, 7, 9), array(8, 6, 4, 9), array(1, 3, 5, 6)); // Function call swapUpperToLower($arr); // This code is contributed by Ajit. ?> |
chevron_right
filter_none
Output :
2 4 8 1 3 5 6 3 5 7 4 5 6 9 9 6
Recommended Posts:
- Maximum size square sub-matrix with all 1s
- Print a given matrix in spiral form
- Search in a row wise and column wise sorted matrix
- A Boolean Matrix Question
- Matrix Chain Multiplication | DP-8
- Print unique rows in a given boolean matrix
- Inplace (Fixed space) M x N size matrix transpose | Updated
- Maximum sum rectangle in a 2D matrix | DP-27
- Zigzag (or diagonal) traversal of Matrix
- Divide and Conquer | Set 5 (Strassen's Matrix Multiplication)
- Print all possible paths from top left to bottom right of a mXn matrix
- Count all possible paths from top left to bottom right of a mXn matrix
- Printing brackets in Matrix Chain Multiplication Problem
- Create a matrix with alternating rectangles of O and X
- Given an n x n square matrix, find sum of all sub-squares of size k x k
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



