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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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



Output :

2 4 8 1 
3 5 6 3 
5 7 4 5 
6 9 9 6 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : vt_m, jit_t, Shivi_Aggarwal



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.