-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.java
72 lines (54 loc) · 1.28 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
69
70
71
72
import java.util.Scanner;
class Matrix
{
public static void main(String args[])
{
int r1, r2,c1,c2,i,j,k,sum;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows of matrix1");
r1 = sc.nextInt();
System.out.println("Enter the number columns of matrix 1");
c1 = sc.nextInt();
System.out.println("Enter the number of rows of matrix2");
r2 = sc.nextInt();
System.out.println("Enter the number of columns of matrix 2");
c2 = sc.nextInt();
if(c1==r2)
{
int mat1[][] = new int[r1][c1];
int mat2[][] = new int[r2][c2];
int res[][] = new int[r1][c2];
System.out.println("Enter the elements of matrix1");
for ( i= 0 ; i < r1 ; i++ )
{
for ( j= 0 ; j < c1 ;j++ )
mat1[i][j] = sc.nextInt();
}
System.out.println("Enter the elements of matrix2");
for ( i= 0 ; i < r2 ; i++ )
{
for ( j= 0 ; j < c2 ;j++ )
mat2[i][j] = sc.nextInt();
}
System.out.println("\n\noutput matrix:-");
for ( i= 0 ; i < r1 ; i++ )
for ( j= 0 ; j <c2;j++)
{
sum=0;
for ( k= 0 ; k <r2;k++ )
{
sum +=mat1[i][k]*mat2[k][j] ;
}
res[i][j]=sum;
}
for ( i= 0 ; i < r1; i++ )
{
for ( j=0 ; j < c2;j++ )
System.out.print(res[i][j]+" ");
System.out.println();
}
}
else
System.out.print("multipication does not exist ");
}
}