Related Articles
Java Program to Accept a Matrix of Order M x N & Interchange the Diagonals
• Difficulty Level : Expert
• Last Updated : 04 Nov, 2020

Problem Description: Write a Java program that accepts a matrix of M × N order and then interchange diagonals of the matrix.

Steps:

1. We can only interchange diagonals for a square matrix.

2. Create a square matrix of size [M × M].

3. Check the matrix is a square matrix or not. If the matrix is square then follow step 3 else terminate the program.

4. Apply logic for interchange diagonal of the matrix some logic is given below.

Method 1: Swap element a[i][i] and a[i][n – i -1]

for (j = 0; j < m; j++) {

temp = a[j][j];

a[j][j] = a[j][n – 1 – j];

a[j][n – 1 – j] = temp;

}

Example:

## Java

 `//  Java Program to Accept a Matrix of Order M x N &``//  Interchange the Diagonals`` ` `import` `java.util.Scanner;``public` `class` `InterchangeDiagonals {``    ``public` `static` `void` `main(String[] args)``    ``{``        ``// declare variable``        ``int` `m, n, i, j, temp;`` ` `        ``// create a object of scanner class``        ``Scanner sc = ``new` `Scanner(System.in);``        ``System.out.print(``"Enter number of rows "``);`` ` `        ``// take number of rows``        ``m = sc.nextInt();`` ` `        ``System.out.print(``"Enter number of columns "``);`` ` `        ``// take number of columns``        ``n = sc.nextInt();`` ` `        ``// declare a mxn order array``        ``int` `a[][] = ``new` `int``[m][n];`` ` `        ``// if block it's execute when m is equals to n``        ``if` `(m == n) {``            ``System.out.println(``                ``"Enter all the values of matrix "``);`` ` `            ``// take the matrix inputs``            ``for` `(i = ``0``; i < m; i++) {``                ``for` `(j = ``0``; j < n; j++) {``                    ``a[i][j] = sc.nextInt();``                ``}``            ``}`` ` `            ``System.out.println(``"original Matrix:"``);`` ` `            ``// print the original matrix``            ``for` `(i = ``0``; i < m; i++) {``                ``for` `(j = ``0``; j < n; j++) {``                    ``System.out.print(a[i][j] + ``" "``);``                ``}``                ``System.out.println(``""``);``            ``}`` ` `            ``// perfom interchange``            ``for` `(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` `(i = ``0``; i < m; i++) {``                ``for` `(j = ``0``; j < n; j++) {``                    ``System.out.print(a[i][j] + ``" "``);``                ``}``                ``System.out.println(``""``);``            ``}``        ``}`` ` `        ``// else block it's only execute when m is not equals``        ``// to n``        ``else` `{``            ``System.out.println(``"Rows not equal to columns"``);``        ``}``    ``}``}`

Output:

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

Example 2:

## Java

 `//  Java Program to Accept a Matrix of Order MxN &``//  Interchange the Diagonals`` ` `import` `java.util.Scanner;``public` `class` `InterchangeDiagonals {``    ``public` `static` `void` `main(String[] args)``    ``{``        ``// declare variable``        ``int` `m, n, i, j, temp;`` ` `        ``// create a object of scanner class``        ``Scanner sc = ``new` `Scanner(System.in);``        ``System.out.print(``"Enter number of rows "``);`` ` `        ``// take number of rows``        ``m = sc.nextInt();`` ` `        ``System.out.print(``"Enter number of columns "``);`` ` `        ``// take number of columns``        ``n = sc.nextInt();`` ` `        ``// declare a mxn order array``        ``int` `a[][] = ``new` `int``[m][n];`` ` `        ``// if block it's execute when m is equals to n``        ``if` `(m == n) {``            ``System.out.println(``                ``"Enter all the values of matrix "``);`` ` `            ``// take input matrix``            ``for` `(i = ``0``; i < m; i++) {``                ``for` `(j = ``0``; j < n; j++) {``                    ``a[i][j] = sc.nextInt();``                ``}``            ``}`` ` `            ``System.out.println(``"original Matrix:"``);`` ` `            ``// print original matrix``            ``for` `(i = ``0``; i < m; i++) {``                ``for` `(j = ``0``; j < n; j++) {``                    ``System.out.print(a[i][j] + ``" "``);``                ``}``                ``System.out.println(``""``);``            ``}`` ` `            ``// perfoming interchange``            ``for` `(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` `(i = ``0``; i < m; i++) {``                ``for` `(j = ``0``; j < n; j++) {``                    ``System.out.print(a[i][j] + ``" "``);``                ``}``                ``System.out.println(``""``);``            ``}``        ``}`` ` `        ``// else block it's only execute when m is not equals``        ``// to n``        ``else` `{``            ``System.out.println(``"Rows not equal to columns"``);``        ``}``    ``}``}`

Output:

Enter number of rows 2

Enter number of columns 1

Enter all the values of matrix

1

2

Rows not equal to columns

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up