Header Ads Widget

Responsive Advertisement

Transpose of a Matrix

 



 import java.util.*;

public class twoDArray {
    public static void main(String[] args) {


        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the size of Row : ");
        int row = sc.nextInt();
        System.out.print("Enter the size of Column : ");
        int col = sc.nextInt();

        int numbers[][] = new int[row][col];
        System.out.println();

        // input
        System.out.println("Enter the element of this Matrix");
        for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
        numbers[i][j] = sc.nextInt();
        }

        }
        System.out.println("The Matrix is : ");
        for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
        System.out.print(numbers[i][j] + " ");
        }
        System.out.println();
        }

        System.out.println("The Transpose Matrix is : ");
        for (int j = 0; j < col; j++) {
        for (int i = 0; i < row; i++) {
        System.out.print(numbers[i][j] + " ");
        }
        System.out.println();
        }
  }
}

Post a Comment

0 Comments