Java Program to Interchange any two Rows & Columns in the given Matrix

«
»
This is a Java Program to Interchange any two Rows & Columns in the given Matrix.

Enter the elements of array as input. Now select the option whether you want to interchange rows or columns. We use loops to interchange columns or rows respectively.

Here is the source code of the Java Program to Interchange any two Rows & Columns in the given Matrix. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

advertisement
  1. import java.util.Scanner;
  2. public class Interchange 
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         int p, q, n, x , y, temp = 0, k = 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.         int a[][] = new int[p][q];
  13.         System.out.println("Enter all the elements of matrix:");
  14.         for (int i = 0; i < p; i++) 
  15.         {
  16.             for (int j = 0; j < q; j++) 
  17.             {
  18.                 a[i][j] = s.nextInt();
  19.             }
  20.         }
  21.         System.out.println("Given Matrix:");
  22.         for (int i = 0; i < p; i++) 
  23.         {
  24.             for (int j = 0; j < q; j++) 
  25.             {
  26.                 System.out.print(a[i][j] + " ");
  27.             }
  28.             System.out.println("");
  29.         }
  30.         while (true) 
  31.         {
  32.             System.out.println("Enter 1 to interchange rows");
  33.             System.out.println("Enter 2 to interchange columns");
  34.             System.out.println("Enter 3 to Exit");
  35.             n=s.nextInt();
  36.             switch (n) 
  37.             {
  38.                 case 1:
  39.                 System.out.println("Enter the row numbers:");
  40.                 x = s.nextInt();
  41.                 y = s.nextInt();
  42.                 for(int i = 0; i < p; i++)
  43.                 {
  44.                     temp = a[(x-1)][i];
  45.                     a[x-1][i] = a[y-1][i];
  46.                     a[y-1][i] = temp;
  47.                 }
  48.                 System.out.println("Matrix after interchanging rows:"+x +" and "+y);
  49.                 for (int i = 0; i < p; i++) 
  50.                 {
  51.                     for (int j = 0; j < q; j++) 
  52.                     {
  53.                         System.out.print(a[i][j] + " ");
  54.                     }
  55.                 System.out.println("");
  56.                 }
  57.                 break;
  58.                 case 2:
  59.                 System.out.println("Enter the column numbers:");
  60.                 x = s.nextInt();
  61.                 y = s.nextInt();
  62.                 for(int i = 0; i < p; i++)
  63.                 {
  64.                     temp = a[i][(x-1)];
  65.                     a[i][x-1] = a[i][(y-1)];
  66.                     a[i][y-1] = temp;
  67.                 }
  68.                 System.out.println("Matrix after interchanging columns:"+x +" and "+y);
  69.                 for (int i = 0; i < p; i++) 
  70.                 {
  71.                     for (int j = 0; j < q; j++) 
  72.                     {
  73.                         System.out.print(a[i][j] + " ");
  74.                     }
  75.                 System.out.println("");
  76.                 }
  77.                 break;
  78.           	case 3:
  79.                 System.exit(0);
  80.             }
  81.         }
  82.     }
  83. }

Output:

$ javac Interchange.java
$ java Interchange 
 
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 
Enter 1 to interchange rows
Enter 2 to interchange columns
Enter 3 to Exit
1
Enter the row numbers:
2
3
Matrix after interchanging rows:2 and 3
1 2 3 
7 8 9 
4 5 6 
Enter 1 to interchange rows
Enter 2 to interchange columns
Enter 3 to Exit
2
Enter the column numbers:
1
2
Matrix after interchanging columns:1 and 2
2 1 3 
8 7 9 
5 4 6 
Enter 1 to interchange rows
Enter 2 to interchange columns
Enter 3 to Exit
3

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

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

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!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter