-
Notifications
You must be signed in to change notification settings - Fork 1
/
Addition_Subtraction_Multiplication.c
113 lines (98 loc) · 2.58 KB
/
Addition_Subtraction_Multiplication.c
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdio.h>
int main(){
int m, n, e, f, z, a[20][20], b[20][20], i, j, k, sum[20][20], sub[20][20], pdt[20][20], ch;
printf("NOTE :\n* For Addition or Subtraction, the two matrices must have the same order \n* For Multiplication, the number of Columns of the first matrix must be equal to the number of Rows of the second matrix\n\n");
printf("\nEnter the no. of Rows of the first matrix : \n");
scanf("%d", &m);
printf("Enter the no. of Columns of the first matrix : \n");
scanf("%d", &n);
printf("Enter the Data Elements of the first matrix : \n");
for(i = 0; i < m; i++){
for(j = 0; j < n; j++){
scanf("%d", &a[i][j]);
}
}
printf("\nFirst Matrix :");
for(i = 0; i < m; i++){
printf("\n");
for(j = 0; j < n; j++){
printf("%d \t", a[i][j]);
}
}
printf("\n\n\nEnter the no. of Rows of the second matrix : \n");
scanf("%d",&e);
printf("Enter the no. of Columns of the second matrix : \n");
scanf("%d",&f);
printf("Enter the Data Elements of the second matrix : \n");
for(i = 0; i < e; i++){
for(j = 0; j < f; j++){
scanf("%d", &b[i][j]);
}
}
printf("\nSecond Matrix :");
for(i = 0; i < e; i++){
printf("\n");
for(j = 0; j < f; j++){
printf("%d \t", b[i][j]);
}
}
printf("\n\n\nEnter 1 for Addition\n");
printf("Enter 2 for Multiplication\n");
printf("Enter 3 for Subtraction\n");
scanf("%d", &ch);
switch(ch){
case 1: /*Addition*/{
if (m == e && n == f){
printf("\n\nSum of the two matrices :");
for(i = 0; i < m; i++){
printf("\n");
for(j = 0; j < n; j++){
sum[i][j] = a[i][j] + b[i][j];
printf("%d \t", sum[i][j]);
}
}
}
else{
printf("\n\nAddition is not possible since the two matrices do not have the same order");
}
break;
}
case 2: /*Multiplication*/{
if(n != e){
printf("\n\nThe matrices cannot be multiplied");
break;
}
int k;
printf("\n\nThe resultant matrix is : \n");
for(i = 0; i < m; i++){
for(j = 0; j < f; j++){
for(k = 0; k < e; k++){
z += a[i][k] * b[k][j];
}
pdt[i][j] = z;
printf("%d ", pdt[i][j]);
z = 0;
}
printf("\n");
}
break;
}
case 3: /*Subtraction*/{
if (m == e && n == f){
printf("\n\nDifference of the two matrices :");
for(i = 0; i < m; i++){
printf("\n");
for(j = 0; j < f; j++){
sub[i][j] = a[i][j] - b[i][j];
printf("%d \t", sub[i][j]);
}
}
}
else{
printf("\nSubtraction is not possible since the two matrices do not have the same order");
}
break;
}
}
return 0;
}