-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path26.c
45 lines (39 loc) · 1.18 KB
/
26.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
//Program to add two matrices
#include <stdio.h>
int main() {
int r, c;
printf("Enter the number of rows : ");
scanf("%d", &r);
printf("Enter the number of columns : ");
scanf("%d", &c);
int a[r][c], b[r][c], sum[r][c];
//Accepting the first input matrix
printf("\nEnter elements of 1st matrix:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
//Accepting the second input matrix
printf("\nEnter elements of 2nd matrix:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
// Adding two matrices
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
// Printing the result
printf("\nSum of two matrices: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}
return 0;
}