Computer Science

Write a program in Java to create a 4 x 4 matrix. Now, swap the elements of 0th row with 3rd row correspondingly. Display the result after swapping.

Sample Input

55332614
81863110
58641712
22142325

Sample Output

22142325
81863110
58641712
55332614

Java

Java Arrays

17 Likes

Answer

import java.util.Scanner;

public class KboatDDARowSwap
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int arr[][] = new int[4][4];
        
        System.out.println("Enter elements of 4x4 array ");
        for (int i = 0; i < 4; i++) {
            System.out.println("Enter Row "+ (i+1) + " :");
            for (int j = 0; j < 4; j++) {
                arr[i][j] = in.nextInt();
            }
        }
        
        System.out.println("Input Array:");
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.print(arr[i][j] + "\t");
            }
            System.out.println();
        }
        
        //Swap 0th and 3rd rows
        for (int j = 0; j < 4; j++) {
            int t = arr[0][j];
            arr[0][j] = arr[3][j];
            arr[3][j] = t;
        }
        
        System.out.println("Swapped Array:");
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                System.out.print(arr[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

Output

Answered By

5 Likes


Related Questions