forked from sarjus/OODP-KTU-Java-Source-Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixTranspose.java
50 lines (45 loc) · 1.38 KB
/
MatrixTranspose.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* The MatrixTranspose program implements an application that
* find the transpose of the given matrix and
*
* @author Sarju S
* @version 1.0
* @since 2020-10-01 */
package com.sjcet.basicPrograms;
import java.util.Scanner;
public class MatrixTranspose {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows in the 2D matrix:");
int noOfRows = sc.nextInt();
System.out.println("Enter the number of columns in the 2D matrix:");
int noOfcolumns = sc.nextInt();
int [][]matrix = new int[noOfRows][noOfcolumns];
System.out.println("Enter the elements");
for(int i=0;i<noOfRows;i++) {
for(int j=0;j<noOfcolumns;j++) {
matrix[i][j] = sc.nextInt();
}//end for j
}//end for i
System.out.println("The matrix is:");
for(int i=0;i<noOfRows;i++) {
for(int j=0;j<noOfcolumns;j++) {
System.out.print(matrix[i][j]+"\t");
}//end for j
System.out.println();
}// end for i
int [][]Tmatrix = new int[noOfcolumns][noOfRows];
for(int i=0;i<noOfcolumns;i++) {
for(int j=0;j<noOfRows;j++) {
Tmatrix[i][j] = matrix[j][i];
}
}
System.out.println("The Transpose of the given matrix is:");
for(int i=0;i<noOfcolumns;i++) {
for(int j=0;j<noOfRows;j++) {
System.out.print(Tmatrix[i][j]+"\t");
}
System.out.println();
}
}
}