1//This program is for matrix addition by programiz.com
2#include <stdio.h>
3int main() {
4 int r, c, a[100][100], b[100][100], sum[100][100], i, j;
5 printf("Enter the number of rows (between 1 and 100): ");
6 scanf("%d", &r);
7 printf("Enter the number of columns (between 1 and 100): ");
8 scanf("%d", &c);
9
10 printf("\nEnter elements of 1st matrix:\n");
11 for (i = 0; i < r; ++i)
12 for (j = 0; j < c; ++j) {
13 printf("Enter element a%d%d: ", i + 1, j + 1);
14 scanf("%d", &a[i][j]);
15 }
16
17 printf("Enter elements of 2nd matrix:\n");
18 for (i = 0; i < r; ++i)
19 for (j = 0; j < c; ++j) {
20 printf("Enter element a%d%d: ", i + 1, j + 1);
21 scanf("%d", &b[i][j]);
22 }
23
24 // adding two matrices
25 for (i = 0; i < r; ++i)
26 for (j = 0; j < c; ++j) {
27 sum[i][j] = a[i][j] + b[i][j];
28 }
29
30 // printing the result
31 printf("\nSum of two matrices: \n");
32 for (i = 0; i < r; ++i)
33 for (j = 0; j < c; ++j) {
34 printf("%d ", sum[i][j]);
35 if (j == c - 1) {
36 printf("\n\n");
37 }
38 }
39
40 return 0;
41}
42
1#include <stdio.h>
2int main(){
3int row, column, mat1[100][100], mat2[100][100], sum[100][100], i, j;
4printf("Enter the number of rows and columns : \n");
5scanf("%d %d", &row, &column);
6printf("\nInput Matrix 1 elements : ");
7for(i=0; i<row; ++i)
8for(j=0; j<column; ++j)
9{
10scanf("%d",&mat1[i][j]);
11}
12printf("\nMatrix 1\n");
13for(i=0;i<row;i++)
14{
15for(j=0;j<column;j++)
16{
17printf("%d",mat1[i][j]);
18}
19printf("\n");
20}
21
22printf("\nInput Matrix 2 elements : ");
23for(i=0; i<row; ++i)
24for(j=0; j<column; ++j)
25{
26scanf("%d", &mat2[i][j]);
27}
28printf("\nMatrix 2\n");
29for(i=0;i<row;i++)
30{
31for(j=0;j<column;j++)
32{
33printf("%d",mat1[i][j]);
34}
35printf("\n");
36}
37// Adding Two matrices
38printf("\nAdded Matrix\n");
39for(i=0;i<row;++i)
40for(j=0;j<column;++j)
41{
42sum[i][j]=mat1[i][j]+mat2[i][j];
43}
44
45// print the result
46
47for(i=0;i<row;++i)
48for(j=0;j<column;++j)
49{
50printf("%d ",sum[i][j]);
51
52if(j==column-1)
53{
54printf("\n");
55}
56}
57
58return 0;
59}
60/*©VinCoD*/
1#include <stdio.h>
2void enterData(int first[][10], int second[][10], int r1, int c1, int r2, int c2);
3void multiplyMatrices(int first[][10], int second[][10], int multResult[][10], int r1, int c1, int r2, int c2);
4void display(int mult[][10], int r1, int c2);
5
6int main() {
7 int first[10][10], second[10][10], mult[10][10], r1, c1, r2, c2;
8 printf("Enter rows and column for the first matrix: ");
9 scanf("%d %d", &r1, &c1);
10 printf("Enter rows and column for the second matrix: ");
11 scanf("%d %d", &r2, &c2);
12
13 // Taking input until columns of the first matrix is equal to the rows of the second matrix
14 while (c1 != r2) {
15 printf("Error! Enter rows and columns again.\n");
16 printf("Enter rows and columns for the first matrix: ");
17 scanf("%d%d", &r1, &c1);
18 printf("Enter rows and columns for the second matrix: ");
19 scanf("%d%d", &r2, &c2);
20 }
21
22 // Function to take matrices data
23 enterData(first, second, r1, c1, r2, c2);
24
25 // Function to multiply two matrices.
26 multiplyMatrices(first, second, mult, r1, c1, r2, c2);
27
28 // Function to display resultant matrix after multiplication.
29 display(mult, r1, c2);
30
31 return 0;
32}
33
34void enterData(int first[][10], int second[][10], int r1, int c1, int r2, int c2) {
35 printf("\nEnter elements of matrix 1:\n");
36
37 for (int i = 0; i < r1; ++i) {
38 for (int j = 0; j < c1; ++j) {
39 printf("Enter a%d%d: ", i + 1, j + 1);
40 scanf("%d", &first[i][j]);
41 }
42 }
43 printf("\nEnter elements of matrix 2:\n");
44
45 for (int i = 0; i < r2; ++i) {
46 for (int j = 0; j < c2; ++j) {
47 printf("Enter b%d%d: ", i + 1, j + 1);
48 scanf("%d", &second[i][j]);
49 }
50 }
51}
52
53void multiplyMatrices(int first[][10], int second[][10], int mult[][10], int r1, int c1, int r2, int c2) {
54
55 // Initializing elements of matrix mult to 0.
56 for (int i = 0; i < r1; ++i) {
57 for (int j = 0; j < c2; ++j) {
58 mult[i][j] = 0;
59 }
60 }
61
62 // Multiplying first and second matrices and storing in mult.
63 for (int i = 0; i < r1; ++i) {
64 for (int j = 0; j < c2; ++j) {
65 for (int k = 0; k < c1; ++k) {
66 mult[i][j] += first[i][k] * second[k][j];
67 }
68 }
69 }
70}
71
72void display(int mult[][10], int r1, int c2) {
73
74 printf("\nOutput Matrix:\n");
75 for (int i = 0; i < r1; ++i) {
76 for (int j = 0; j < c2; ++j) {
77 printf("%d ", mult[i][j]);
78 if (j == c2 - 1)
79 printf("\n");
80 }
81 }
82}
83