-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram_55.c
More file actions
62 lines (58 loc) · 1.68 KB
/
Copy pathProgram_55.c
File metadata and controls
62 lines (58 loc) · 1.68 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
// Program 55: Write a program to perform matrix multiplication.
#include<stdio.h>
int main(){
int row1,row2,col1,col2;
printf("Enter the number of row for matrix 1 : ");
scanf("%d",&row1);
printf("Enter the number of row for matrix 2 : ");
scanf("%d",&row2);
printf("Enter the number of col for matrix 1 : ");
scanf("%d",&col1);
printf("Enter the number of col for matrix 2 : ");
scanf("%d",&col2);
// Check condition
if (col1 != row2) {
printf("Matrix multiplication not possible.");
return 0;
}
int matrix1[row1][col1];
int matrix2[row2][col2];
int result[row1][col2];
// Input matrix 1
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col1; j++) {
printf("Enter matrix1[%d][%d]: ", i, j);
scanf("%d", &matrix1[i][j]);
}
}
// Input matrix 2
for (int i = 0; i < row2; i++) {
for (int j = 0; j < col2; j++) {
printf("Enter matrix2[%d][%d]: ", i, j);
scanf("%d", &matrix2[i][j]);
}
}
// Initialize result matrix to 0
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
result[i][j] = 0;
}
}
// Matrix multiplication logic
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
for (int k = 0; k < col1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
// Print result matrix
printf("Resultant Matrix:\n");
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}