Dark Mode On/Off

# Java Program to accept a Matrix of order M*N and Interchange the Diagonals

In this tutorial, we will learn how to accept a matrix of order M*N and interchange the diagonals. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.

Input: Enter the matrix Elements:

1 2 3

6 5 4

7 8 9

Output:

3 2 1

4 5 6

9 8 7

## Program 1: Interchange the Diagonals of a Matrix

In this program, we will see how to accept the matrix of order M*N and interchange the diagonals with user-defined values.

### Algorithm

1. Start
2. Declare variables for the matrix size.
3. Ask the user to initialize the matrix rows and columns
4. Check if the number of rows and columns are equal or not.
5. If equal, then ask the user to initialize the matrix.
6. Print the original matrix.
7. Swap the diagonal elements.
8. Print the interchanged matrix.
9. If rows and columns are not equal, print the same message.
10. Stop

Below is the code for the same.

``````//Java Program to interchange the diagonals*/
import java.util.Scanner;

public class Main
{
public static void main(String[] args)
{
// declare variables
int m, n, temp;

// To take input from the user
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows ");

// Initialize the number of rows
m = sc.nextInt();

System.out.println("Enter number of columns ");

// Initialize the number of columns
n = sc.nextInt();

// declare a mxn order array
int a[][] = new int[m][n];

// Interchange the diagonals only when it is a square matrix
if (m == n)
{
System.out.println("Enter all the values of matrix ");

// Initialize the matrix elements
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = sc.nextInt();
}
}

System.out.println("Original Matrix:");

// print the original matrix
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println("");
}

// Interchange the diagonals by swapping
for (int j = 0; j < m; j++)
{
temp = a[j][j];
a[j][j] = a[j][n - 1 - j];
a[j][n - 1 - j] = temp;
}
System.out.println("After interchanging diagonals of matrix ");

// print interchanged matrix
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println("");
}
}
else
{
System.out.println("Rows not equal to columns");
}
}
}``````

Enter number of rows 3
Enter number of columns 3
Enter all the values of matrix 1 2 3 4 5 6 7 8 9
Original Matrix:
1 2 3
4 5 6
7 8 9
After interchanging diagonals of matrix
3 2 1
4 5 6
9 8 7

## Program 2: Interchange the Diagonals of a Matrix

In this program, we will see how to accept the matrix of order M*N and interchange the diagonals with pre-defined values.

### Algorithm

1. Start
2. Declare and initialize the matrix size.
3. Check whether the number of rows and columns are equal or not.
4. If equal, then initialize the elements of the matrix.
5. Print the original matrix.
6. Call a method to interchange the diagonals.
7. Swap the diagonal elements.
8. Print the interchanged matrix.
9. If rows and columns are not equal, print the same message.
10. Stop

Below is the code for the same.

``````//Java Program to interchange the diagonals*/
import java.util.*;

public class Main
{
//Method to interchange the diagonals
static void interchangeDiagonals(int arr[][])
{
int temp=0;
int m=arr.length;     //Variable to store the number of rows
int n=arr[0].length;  //Variable to store the number of columns
System.out.println("Original Matrix:");

// print the original matrix
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println("");
}

// Interchange the diagonals by swapping
for (int j = 0; j <m; j++)
{
temp = arr[j][j];
arr[j][j] = arr[j][n - 1 - j];
arr[j][n - 1 - j] = temp;
}
System.out.println("After interchanging diagonals of matrix ");

// print interchanged matrix
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println("");
}
}
public static void main(String[] args)
{
// declare variables
int rows=3, columns=3;
// Interchange the diagonals only when it is a square matrix
if (rows == columns)
{
int arr[][]  = { { 2, 9, 8 }, { 7, 6, 4 }, { 3, 9, 2 } };   //Matrix Declaration
interchangeDiagonals(arr);
}
else
{
System.out.println("Rows not equal to columns");
}
}
}``````

Original Matrix:
2 9 8
7 6 4
3 9 2
After interchanging diagonals of the matrix
8 9 2
7 6 4
2 9 3

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.