You are on page 1of 6

« Prev Page Next Page »

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

for (int i = 0. } 81. } 68. System. { 64. System. 76. } 82.nextInt().out.println("Matrix after interchanging rows:"+x +" and "+y). break. 61. 60. x = s. i++) 50. 58. for(int i = 0.out. 67.nextInt(). i++) 70. i < p. { 73. System. 62. } 75. for (int j = 0.out. } 57. 78. case 3: 79. y = s.com . 66. a[i][y-1] = temp. for (int i = 0. { 51. j++) 52.println("Enter the column numbers:"). break. System. System. 80. { 53.println(""). 56. } 55.out. System. a[i][x-1] = a[i][(y-1)]. } Output: converted by W eb2PDFConvert. } 48. 49.print(a[i][j] + " "). i < p. for (int j = 0.out. } 77.out. j < q. i < p. temp = a[i][(x-1)]. 69. } 83.47. { 71.out. 74. System. case 2: 59.println("Matrix after interchanging columns:"+x +" and "+y). 54. j++) 72.print(a[i][j] + " "). 65. System. j < q.println("").exit(0). i++) 63.

C Programming Examples on Linked List 2. Java Programming Examples on Collection API 3.com .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.Java Program to Add Two MXN Matrix from User Input » Next Page . Data Structures and Algorithms. Java Programming Examples on Hard Graph Problems & Algorithms converted by W eb2PDFConvert. $ javac Interchange. Here’s the list of Best Reference Books in Java Programming.Java Program to Display Upper Triangular Matrix Deep Dive @ Sanfoundry: 1. « Prev Page . C Programming Examples 4.

Kernel Programming. IBM. Our Founder has trained employees of almost all Top Companies in India such as VMware. Advanced C Programming. Tata-Elxsi. Accenture. C# Programming Examples on Matrix 10.Admin Linux Fundamentals Advanced C Training Linux-C Debugging System Programming Network Programming converted by W eb2PDFConvert. HP. Ericsson. Java Programming Examples on Numerical Problems & Algorithms 7. TCS. SAP Labs. Stay connected with him below: LinkedIn | Facebook | Twitter | Google+ Subscribe Sanfoundry Newsletter and Posts Name* Email* Best Careers Developer Tracks SAN Developer Linux Kernel Developer Linux Driver Developer Linux Network Developer Live Training Photos Mentoring Software Productivity GDB Assignment Sanfoundry is No. 5. Redhat. Cisco. Motorola. Linux Debugging. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel. Linux Device Drivers. Intuit. Cavium. ST-Micro. SAN Storage Technologies. Wipro. a technology veteran with 20+ years @ Cisco & Wipro. Best Trainings SAN I . Linux Networking. Tata VSNL. Citrix. Mindtree. Oracle. Java Programming Examples on Combinatorial Problems & Algorithms 9. Microsoft. Mphasis. 1 choice for Deep Hands-ON Trainings in SAN. SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Aricent. Samsung. is Founder and CTO at Sanfoundry. Java Programming Examples on Graph Problems & Algorithms 6. HCL. Chelsio. Cognizant and Startups. LG-Soft.Technology SAN II .com . He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. Linux Storage & Cluster Administration. Java Programming Examples on Data-Structures 8. Linux & C. C Programming Examples on Matrix Manish Bhojasia. HSBC. Siemens. Symantec.

Linux Threads Kernel Programming Kernel Debugging Linux Device Drivers Best Reference Books Computer Science Books Algorithm & Programming Books Electronics Engineering Books Electrical Engineering Books Chemical Engineering Books Civil Engineering Books Mechanical Engineering Books Industrial Engineering Books Instrumentation Engg Books Metallurgical Engineering Books All Stream Best Books Questions and Answers 1000 C Questions & Answers 1000 C++ Questions & Answers 1000 C# Questions & Answers 1000 Java Questions & Answers 1000 Linux Questions & Answers 1000 Python Questions 1000 PHP Questions & Answers 1000 Hadoop Questions Cloud Computing Questions Computer Science Questions All Stream Questions & Answers India Internships Computer Science Internships Instrumentation Internships Electronics Internships Electrical Internships Mechanical Internships Industrial Internships Systems Internships Chemical Internships Civil Internships IT Internships All Stream 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 .

com .converted by W eb2PDFConvert.