Sei sulla pagina 1di 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
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

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.


Heres the list of Best Reference Books in Java Programming, Data Structures and Algorithms.
Prev Page - Java Program to Add Two MXN Matrix from User Input
Next Page - Java Program to Display Upper Triangular Matrix

Deep Dive @ Sanfoundry:


1. C Programming Examples on Linked List
2. Java Programming Examples on Collection API
3. C Programming Examples
4. Java Programming Examples on Hard Graph Problems & Algorithms

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+

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. 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

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
converted by W eb2PDFConvert.com

Potrebbero piacerti anche