bluej Programs
2.Write a program to design a upside down Mirror Matrix.
import java.util.*;
class mirrormtrx_upsdn
{
Scanner sc=new Scanner(System.in);
int ar[][],br[][],m;
void input()
{
System.out.println("Enter the size of the matrix:");
m=sc.nextInt();
if(m<=2 && m>=6)
System.exit(0);
ar=new int[m][m];
br=new int[m][m];
for(int i=0;i<m; i++)
{
System.out.println("Enter "+m+" number of elements of row"+i);
for(int j=0;j<m;j++)
{
ar[i][j]=sc.nextInt();
}
}
}
void display()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(ar[i][j]+" ");
}
System.out.println();
}
}
void upside_down()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
br[m-1-i][j]=ar[i][j];
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(br[i][j]+" ");
}
System.out.println();
}
}
void main()
{
mirrormtrx_upsdn obj=new mirrormtrx_upsdn();
obj.input();
System.out.println("Original array:");
obj.display();
System.out.println("Upside down matrix is:");
obj.upside_down();
}
}
Output:
Enter the size of the matrix: 5 Enter 5 number of elements of row0 34 87 12 10 11 Enter 5 number of elements of row1 22 33 44 55 66 Enter 5 number of elements of row2 99 98 97 67 56 Enter 5 number of elements of row3 32 13 42 53 63 Enter 5 number of elements of row4 14 53 36 76 87 Original array: 34 87 12 10 11 22 33 44 55 66 99 98 97 67 56 32 13 42 53 63 14 53 36 76 87 Upside down matrix is: 14 53 36 76 87 32 13 42 53 63 99 98 97 67 56 22 33 44 55 66 34 87 12 10 11
ADVERTISEMENT
