KnowledgeBoat Logo

Computer Science

Write a program in Java to enter natural numbers in a double dimensional array m x n (where m is the number of rows and n is the number of columns). Display the new matrix in such a way that the new matrix is the mirror image of the original matrix.

815918
91076
1081113
12161719
Sample Input
189158
67109
1311810
19171612
Sample Output

Java

Java Arrays

18 Likes

Answer

import java.util.Scanner;

public class KboatSDAMirrorImage
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number of rows (m): ");
        int m = in.nextInt();
        System.out.print("Enter number of columns (n): ");
        int n = in.nextInt();
        
        int arr[][] = new int[m][n];
        int newArr[][] = new int[m][n];
        
        System.out.println("Enter array elements");
        for (int i = 0; i < m; i++) {
            System.out.println("Enter Row "+ (i+1) + " :");
            for (int j = 0; j < n; j++) {
                arr[i][j] = in.nextInt();
            }
        }
        
        System.out.println("Input Array:");
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(arr[i][j] + "\t");
            }
            System.out.println();
        }
        
        for (int j = 0; j < n; j++) {
            for (int i = 0; i < m; i++) {
                newArr[i][n - 1 - j] = arr[i][j]; 
            }
        }
        
        System.out.println("Mirror Image Array:");
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(newArr[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

Output

BlueJ output of Write a program in Java to enter natural numbers in a double dimensional array m x n (where m is the number of rows and n is the number of columns). Display the new matrix in such a way that the new matrix is the mirror image of the original matrix.

Answered By

6 Likes


Related Questions