-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMatrix.java
68 lines (58 loc) · 2.01 KB
/
Matrix.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.*;
public class Matrix{
int array[][];
int size;
Matrix(int size){
this.size = size;
this.array = new int[size][size];
}
Scanner sc = new Scanner(System.in);
public void get_input(){
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
array[i][j] = sc.nextInt();
}
}
}
public void matrix_mul(Matrix m1,Matrix m2){
for(int i=0;i<m1.size;i++){
for(int j=0;j<m1.size;j++){
for(int k=0;k<m1.size;k++){
array[i][j] += m1.array[i][k] * m2.array[k][j];
}
}
}
}
public void print_matrix(){
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("This is valid for two square matrices of same order ");
System.out.println("Enter the order of both the matrices");
int size;
size = sc.nextInt();
Matrix m1 = new Matrix(size);
Matrix m2 = new Matrix(size);
Matrix m3 = new Matrix(size);
System.out.println("Enter the elements of matrix 1");
m1.get_input();
System.out.println("Enter the elemnets of matrix 2");
m2.get_input();
m3.matrix_mul(m1,m2);
System.out.println("----------------------------------------------");
System.out.println("The Matrix 1 is:");
m1.print_matrix();
System.out.println("----------------------------------------------");
System.out.println("The Matrix 2 is:");
m2.print_matrix();
System.out.println("----------------------------------------------");
System.out.println("The Multiplication of two matrices is:");
m3.print_matrix();
}
}