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.
import java.util.Scanner;
public class Interchange
{
public static void main(String[] args)
{
int p, q, n, x , y, temp = 0, k = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of rows in matrix:");
p = s.nextInt();
System.out.print("Enter number of columns in matrix:");
q = s.nextInt();
int a[][] = new int[p][q];
System.out.println("Enter all the elements of matrix:");
for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
a[i][j] = s.nextInt();
}
}
System.out.println("Given Matrix:");
for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println("");
}
while (true)
{
System.out.println("Enter 1 to interchange rows");
System.out.println("Enter 2 to interchange columns");
System.out.println("Enter 3 to Exit");
n=s.nextInt();
switch (n)
{
case 1:
System.out.println("Enter the row numbers:");
x = s.nextInt();
y = s.nextInt();
for(int i = 0; i < p; i++)
{
temp = a[(x-1)][i];
a[x-1][i] = a[y-1][i];
a[y-1][i] = temp;
}
System.out.println("Matrix after interchanging rows:"+x +" and "+y);
for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println("");
}
break;
case 2:
System.out.println("Enter the column numbers:");
x = s.nextInt();
y = s.nextInt();
for(int i = 0; i < p; i++)
{
temp = a[i][(x-1)];
a[i][x-1] = a[i][(y-1)];
a[i][y-1] = temp;
}
System.out.println("Matrix after interchanging columns:"+x +" and "+y);
for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println("");
}
break;
case 3:
System.exit(0);
}
}
}
}
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 Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Practice BCA MCQs
- Apply for Computer Science Internship
- Check Java Books
- Practice Programming MCQs
- Apply for Java Internship