Java Program to Interchange Diagonals of Matrix

This is a Java Program to Accept a Matrix of Order MxN & Interchange the Diagonals.

Enter the elements of array as input. Now we use loops and if else condition to interchange the two diagonals of the matrix.

Here is the source code of the Java Program to Accept a Matrix of Order MxN & Interchange the Diagonals. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class Interchange_Diagonals
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         int p, q, temp = 0;
  7.         Scanner s = new Scanner(System.in);
  8.         System.out.print("Enter number of rows in matrix:");
  9.         p = s.nextInt();
  10.         System.out.print("Enter number of columns in matrix:");
  11.         q = s.nextInt();
  12.         if (p == q) 
  13.         {
  14.             int a[][] = new int[p][q];
  15.             System.out.println("Enter all the elements of matrix:");
  16.             for (int i = 0; i < p; i++) 
  17.             {
  18.                 for (int j = 0; j < q; j++) 
  19.                 {
  20.                     a[i][j] = s.nextInt();
  21.                 }
  22.             }
  23.             System.out.println("Given Matrix:");
  24.             for (int i = 0; i < p; i++) 
  25.             {
  26.                 for (int j = 0; j < q; j++) 
  27.                 {
  28.                     System.out.print(a[i][j] + " ");
  29.                 }
  30.                 System.out.println("");
  31.              }
  32.             for(int j = 0; j < q; j++)
  33.             {
  34.                 temp = a[j][j];
  35.                 a[j][j] = a[j][q-1-j];
  36.                 a[j][q-1-j] = temp;
  37.             }
  38.             System.out.println("Matrix after interchanging diagonals");
  39.             for (int i = 0; i < p; i++) 
  40.             {
  41.                 for (int j = 0; j < q; j++) 
  42.                 {
  43.                     System.out.print(a[i][j] + " ");
  44.                 }
  45.                 System.out.println("");
  46.             }
  47.         } 
  48.         else 
  49.         {
  50.             System.out.println("Rows not equal to column");
  51.         }
  52.     }
  53. }

Output:

$ javac Interchange_Diagonals.java
$ java Interchange_Diagonals
 
Enter number of rows in matrix:3
Enter number of columns in matrix:3
Enter all the elements of matrix:
1
2
3
4
5
6
7
8
9
Given Matrix:
1 2 3 
4 5 6 
7 8 9 
Matrix after interchanging diagonals
3 2 1 
4 5 6 
9 8 7

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.