Home Show/Hide Menu
Bluej Programs
➜ Test-paper-IT-10
➜ Suggestion Paper, Class-12

bluej Programs

3.Write a program to Enter a matrix of m x n.Print its mirror image.

import java.util.*;
class mirrorImage
{
    Scanner sc=new Scanner(System.in);
    int mir[][],m,n;
    void input()
    {
        System.out.println("Enter total rows and columns");
        m=sc.nextInt();
        n=sc.nextInt();
        mir=new int[m][n];
        for(int i=0;i<m;i++)
        {
            System.out.println("Enter "+n+" number of elements of rows"+i);
            for(int j=0;j<n;j++)
            {
                mir[i][j]=sc.nextInt();
            }
        }
    }
    void display()
    {
        System.out.println("The original array is:");
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                System.out.print(mir[i][j]+" ");
            }
            System.out.println();
        }
    }
    void mirror()
    {
        int b[][]=new int[m][n];
        for(int i=0;i<m;i++)
        {
            for(int j=0,k=n-1;j<n;j++,k--)
            {
                b[i][j]=mir[i][k];
            }
        }
        System.out.println("Mirror array:");
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                System.out.print(b[i][j]+" ");
            }
            System.out.println();
        }
    }
    void main()
    {
        mirrorImage ob=new mirrorImage();
        ob.input();
        ob.display();
        ob.mirror();
    }
}


Output:
Enter total rows and columns
3
4
Enter 4 number of elements of rows0
10
11
12
13
Enter 4 number of elements of rows1
14
15
16
17
Enter 4 number of elements of rows2
18
19
20
21
The original array is:
10 11 12 13 
14 15 16 17 
18 19 20 21 
Mirror array:
13 12 11 10 
17 16 15 14 
21 20 19 18
 
ADVERTISEMENT