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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

