Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions Matrix/Matrix Reverse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import java.io.*;
import java.util.*;
class MatRev
{
private int arr[][];
private int m;
private int n;
public MatRev(int mm, int nn)
{
m=mm;
n = nn;
arr=new int[m][n];
}

public void fillArray( )throws IOException
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter matrix elements::");
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
arr[i][j] = sc.nextInt();
}
}
}

public int reverse(int x)
{
int rev = 0;
for(int i = x; i != 0; i /= 10)
rev = rev * 10 + i % 10;
return rev;
}

public void revMat(MatRev p)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
this.arr[i] [j] = reverse(p.arr[i] [j]);
}
}
}

public void show()
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
System.out. print(arr[i][j] + "\t");
}
System.out.println();
}
}

public static void main(String args[])throws IOException
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows::");
int x = sc.nextInt();
System.out.print("Enter number of columns::");
int y = sc.nextInt();
MatRev obj1 = new MatRev(x, y);
MatRev obj2 = new MatRev(x, y);
obj1.fillArray();
obj2.revMat(obj1);
System.out.println("Original Matrix is::");
obj1.show();
System.out.println("Matrix with reversed elements");
obj2.show();
}
}
/* OUTPUT
Enter number of rows::3
Enter number of columns::3
Enter matrix elements::
2
3
4
10
12
23
43
25
21
Original Matrix is::
2 3 4
10 12 23
43 25 21
Matrix with reversed elements
2 3 4
1 21 32
34 52 12
*/