Skip to content

Commit 0f9d230

Browse files
Matrix Multiplication.c
1 parent 516628f commit 0f9d230

File tree

1 file changed

+220
-0
lines changed

1 file changed

+220
-0
lines changed

Matrix Multiplication.c

+220
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
#include <stdio.h>
2+
3+
#include<stdlib.h>
4+
5+
#include<string.h>
6+
7+
int main (){
8+
9+
10+
// Initialising 1st Matrix
11+
12+
system("clear");
13+
14+
int m1,n1,m2,n2;
15+
16+
printf("No of Rows of 1st Matrix : ");
17+
18+
scanf("%d",&m1);
19+
20+
printf("No of Columns of 1st Matrix : ");
21+
22+
scanf("%d",&n1);
23+
24+
25+
26+
// Initialising the 2nd Matrix
27+
28+
29+
30+
printf("No of Rows of 2nd Matrix : ");
31+
32+
scanf("%d",&m2);
33+
34+
printf("No of Columns of 2nd Matrix : ");
35+
36+
scanf("%d",&n2);
37+
38+
39+
40+
if (n1!=m2)
41+
42+
{
43+
44+
printf("Can't Compute ...\n");
45+
46+
// Checking n1=m2
47+
48+
49+
50+
}
51+
52+
53+
54+
else{
55+
56+
57+
58+
int matrix1[m1][n1];
59+
60+
int matrix2[m2][n2];
61+
62+
// Taking input of 1st Matrix
63+
64+
65+
66+
printf("\n ");
67+
68+
printf("Enter the 1st Matrix : \n\n");
69+
70+
71+
72+
for (int i=1;i<=m1;i++)
73+
74+
{
75+
76+
for (int j=1;j<=n1;j++)
77+
78+
{
79+
80+
printf("\ta[%d][%d] : ",i,j);
81+
82+
scanf("%d",&matrix1[i][j]);
83+
84+
}
85+
86+
}
87+
88+
// Taking input of 2nd Matrix
89+
90+
91+
printf("\n ");
92+
93+
printf("Enter the 2nd Matrix : \n\n");
94+
95+
for (int i=1;i<=m2;i++)
96+
97+
{
98+
99+
for (int j=1;j<=n2;j++)
100+
101+
{
102+
103+
printf("\tb[%d][%d]",i,j);
104+
105+
scanf("%d",&matrix2[i][j]);
106+
107+
}
108+
109+
}
110+
111+
printf("\n");
112+
113+
// Printing 1st Matrix
114+
115+
116+
for (int i=1;i<=m1;i++)
117+
118+
{
119+
120+
printf("\t");
121+
122+
printf("|");
123+
124+
for (int j=1;j<=n1;j++)
125+
126+
{
127+
128+
printf(" %d ",matrix1[i][j]);
129+
130+
131+
132+
133+
134+
}
135+
136+
printf("|");
137+
138+
printf("\n");
139+
140+
}
141+
142+
printf("\n\n");
143+
144+
// Printing 2nd Matrix
145+
146+
147+
for (int i=1;i<=m2;i++)
148+
149+
{
150+
151+
printf("\t");
152+
153+
printf("|");
154+
155+
for (int j=1;j<=n2;j++)
156+
157+
{
158+
159+
printf(" %d ",matrix2[i][j]);
160+
161+
}
162+
163+
printf("|");
164+
165+
printf("\n");
166+
167+
}
168+
169+
170+
171+
172+
173+
int x=1,y=1,sum[m1][n2];
174+
175+
int product[x][y];
176+
177+
for (int k=1;k<=m1;k++)
178+
179+
{
180+
181+
for (int p=1;p<=n2;p++)
182+
183+
{
184+
185+
// product[k][p]=matrix1[k][p]*matrix2[p][k];
186+
187+
//um=sum+product[k][p];
188+
189+
// printf("%d",sum);
190+
191+
product[k][p]=matrix1[k][p]*matrix2[p][k];
192+
193+
sum[k][p]=sum[k][p]+product[k][p];
194+
195+
196+
197+
printf("%d\n",sum[k][p]);
198+
199+
200+
201+
}
202+
203+
204+
205+
}
206+
207+
208+
209+
210+
} // Closing of else Statement
211+
212+
213+
214+
215+
216+
}
217+
218+
219+
220+

0 commit comments

Comments
 (0)