-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMartixMultiplication.java
More file actions
75 lines (65 loc) · 2.07 KB
/
MartixMultiplication.java
File metadata and controls
75 lines (65 loc) · 2.07 KB
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
69
70
71
72
73
74
75
package com.sjcet.basicPrograms;
import java.util.Scanner;
public class MartixMultiplication {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows in the first matrix:");
int r1 = sc.nextInt();
System.out.println("Enter the number of columns in the first matrix:");
int c1 = sc.nextInt();
int [][]firstMatrix = new int[r1][c1];
System.out.println("Enter the elements");
for(int i=0;i<r1;i++) {
for(int j=0;j<c1;j++) {
firstMatrix[i][j] = sc.nextInt();
}//end for j
}//end for i
System.out.println("Enter the number of rows in the second matrix:");
int r2 = sc.nextInt();
System.out.println("Enter the number of columns in the second matrix:");
int c2 = sc.nextInt();
if(r1==c2) {
int [][]secondMatrix = new int[r2][c2];
System.out.println("Enter the elements");
for(int i=0;i<r2;i++) {
for(int j=0;j<c2;j++) {
secondMatrix[i][j] = sc.nextInt();
}//end for j
}//end for i
System.out.println("The first matrix is:");
for(int[] row : firstMatrix) {
for (int column : row) {
System.out.print(column + "\t");
}
System.out.println();
}
System.out.println("The second matrix is:");
for(int[] row : secondMatrix) {
for (int column : row) {
System.out.print(column + "\t");
}
System.out.println();
}
int [][] productMatrix = new int[r1][c2];
//Matrix Multiplication
for(int i=0;i<r1;i++) {
for(int j=0;j<c2;j++) {
productMatrix[i][j]=0;
for(int k=0;k<c1;k++) {
productMatrix[i][j] = productMatrix[i][j]+ firstMatrix[i][k] * secondMatrix[k][j];
}//end k
}//end j
}//end i
System.out.println("The product matrix is:");
for(int[] row : productMatrix) {
for (int column : row) {
System.out.print(column + "\t");
}
System.out.println();
}
}//end if
else {
System.out.println("Matrix Multiplication is not possible");
}
}
}