Professional Documents
Culture Documents
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.
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. }
converted by W eb2PDFConvert.com
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:
converted by W eb2PDFConvert.com
$ javac Interchange.java
$ java Interchange
converted by W eb2PDFConvert.com
5. Java Programming Examples on Graph Problems & Algorithms
6. Java Programming Examples on Numerical Problems & Algorithms
7. Java Programming Examples on Data-Structures
8. Java Programming Examples on Combinatorial Problems & Algorithms
9. C# Programming Examples on Matrix
10. C Programming Examples on Matrix
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He
is Linux Kernel Developer and 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 & Cluster Administration, Advanced C
Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel.
Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+
Name*
Email*
Best Careers
Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of
almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs,
Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi,
Tata VSNL, Mindtree, Cognizant and Startups.
Best Trainings
SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
converted by W eb2PDFConvert.com
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers
India Internships
About Sanfoundry
About Us
Copyright
TOS & Privacy
Jobs
Bangalore Training
Online Training
SAN Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
2011-2017 Sanfoundry
converted by W eb2PDFConvert.com
converted by W eb2PDFConvert.com