You are on page 1of 9

C program to add two matrices

Write a C program to read elements in two matrices and add elements of both matrices. C
program for addition of two matrix. Matrix addition program in C.

Example:
If matrix 1:
123
456
789

And matrix 2:
987
654
321

Sum of both matrix =


10 10 10
10 10 10
10 10 10

Required knowledge:

Basic C programming, For loop, Array, Matrix

Matrix Addition

Matrix addition is a simple process. Addition of two matrices can be done only and only if
both matrices are of same size.

Matrix addition is done element wise (entry wise) i.e. Sum of two matrices A and B of size
mXn is defined by
(A + B) = Aij + Bij (Where 1 ≤ i ≤ m and 1 ≤ j ≤ n )
Program:

1 /**
2 * C program to find sum of two matrices of size 3x3
*/
3
4 #include <stdio.h>
5
6 int main()
7 {
8 int A[3][3], B[3][3], C[3][3];
9 int row, col;
10
/*
11 * Reads elements in first matrix
12 */
13 printf("Enter elements in matrix A of size 3x3: \n");
14 for(row=0; row<3; row++)
{
15 for(col=0; col<3; col++)
16 {
17 scanf("%d", &A[row][col]);
18 }
19 }
20
/*
21 * Reads elements in second matrix
22 */
23 printf("\nEnter elements in matrix B of size 3x3: \n");
24 for(row=0; row<3; row++)
{
25 for(col=0; col<3; col++)
26 {
27 scanf("%d", &B[row][col]);
28 }
29 }
30
/*
31 * Adds both matrices A and B entry wise or element wise
32 * And stores result in matrix C
33 */
34 for(row=0; row<3; row++)
{
35
for(col=0; col<3; col++)
36 {
37 /* Cij = Aij + Bij */
38 C[row][col] = A[row][col] + B[row][col];
39 }
}
40
41 /*
42 * Prints the sum of both matrices A and B
43 */
44 printf("\nSum of matrices A+B = \n");
45 for(row=0; row<3; row++)
{
46 for(col=0; col<3; col++)
47 {
48 printf("%d ", C[row][col]);
49 }
50 printf("\n");
}
51
52 return 0;
53}
54
55
56
57
58
59
60
61
62
63

Output
Enter elements in matrix A of size 3x3:
123
456
789

Enter elements in matrix B of size 3x3:


987
654
321

Sum of matrices A+B =


10 10 10
10 10 10
10 10 10
C program to subtract two matrices
Write a C program to read elements in two matrices and find the difference of two matrices.
Program to subtract two matrices in C.

Example:
If matrix 1:
123
456
789

And matrix 2:
987
654
321

Difference of both matrices =


-8 -6 -4
-2 0 2
4 6 8

Required knowledge:

Basic C programming, For loop, Array, Matrix

Matrix Subtraction:

Matrix subtraction is a simple and easy process. Elements of two matrices can only be
subtracted if and only if both matrices are of same size.

Matrix subtraction is done element wise (entry wise) i.e. Difference of two matrices A and B
of size mXn is defined by
(A - B) = Aij - Bij (Where 1 ≤ i ≤ m and 1 ≤ j ≤ n )

Program:
1 /**
* C program to find difference of two matrices of size 3x3
2 */
3
4 #include <stdio.h>
5
6 int main()
7 {
int A[3][3], B[3][3], C[3][3];
8 int row, col;
9
10 /*
11 * Reads elements in first matrix
12 */
printf("Enter elements in matrix A of size 3x3: \n");
13 for(row=0; row<3; row++)
14 {
15 for(col=0; col<3; col++)
16 {
17 scanf("%d", &A[row][col]);
}
18 }
19
20 /*
21 * Reads elements in second matrix
22 */
23 printf("\nEnter elements in matrix B of size 3x3: \n");
for(row=0; row<3; row++)
24 {
25 for(col=0; col<3; col++)
26 {
27 scanf("%d", &B[row][col]);
}
28 }
29
30 /*
31 * Subtracts both matrices and stores the result in matrix C
32 */
33 for(row=0; row<3; row++)
{
34 for(col=0; col<3; col++)
35 {
36 C[row][col] = A[row][col] - B[row][col];
37 }
}
38
39 /*
40 * Prints the difference of both matrices A and B
41 */
42 printf("\nDifference of two matrices A-B = \n");
43 for(row=0; row<3; row++)
{
44 for(col=0; col<3; col++)
45 {
46 printf("%d ", C[row][col]);
47 }
printf("\n");
48
}
49
50 return 0;
51}
52
53
54
55
56
57
58
59
60
61

Output
Enter elements in matrix A of size 3x3:
123
456
789

Enter elements in matrix B of size 3x3:


987
654
321

Difference of both matrices A-B =


-8 -6 -4
-2 0 2
4 6 8
C program to perform Scalar matrix
multiplication
Write a C program to read elements in a matrix and perform scalar multiplication of matrix.
C program for scalar multiplication of matrix.

Example:
If matrix A=
123
456
789

Output: 2 . A =
2 4 6
8 10 12
14 16 18

Required knowledge:

Basic C programming, For loop, Array, Matrix

Scalar multiplication of matrix

Scalar multiplication of matrix is the simplest and easiest way to multiply matrix. Scalar
multiplication of matrix is defined by.
(cA)ij = c . Aij (Where 1 ≤ i ≤ m and 1 ≤ j ≤ n)

Program:

1 /**
* C program to perform scalar multiplication of matrix
2 */
3
4 #include <stdio.h>
5
6 int main()
7 {
int A[3][3];
8 int num, row, col;
9
10 /*
11 * Reads elements in matrix from user
12 */
printf("Enter elements in matrix of size 3x3: \n");
13 for(row=0; row<3; row++)
14 {
15 for(col=0; col<3; col++)
16 {
17 scanf("%d", &A[row][col]);
}
18 }
19
20 /* Reads number to perform scalar multiplication from user */
21 printf("Enter any number to multiply with matrix A: ");
22 scanf("%d", &num);
23
/*
24 * Performs scalar multiplication of matrix
25 */
26 for(row=0; row<3; row++)
27 {
28 for(col=0; col<3; col++)
{
29 /* (cAij) = c . Aij */
30 A[row][col] = num * A[row][col];
31 }
32 }
33
34 /*
* Prints the result of scalar multiplication of matrix
35 */
36 printf("\nScalar multiplication of matrix A = \n");
37 for(row=0; row<3; row++)
38 {
for(col=0; col<3; col++)
39 {
40 printf("%d ", A[row][col]);
41 }
42 printf("\n");
43 }
44
return 0;
45}
46
47
48
49
50
51
52
53
54
Output
Enter elements in matrix of size 3x3:
123
456
789
Enter any number to multiply with matrix A: 2

Scalar multiplication of matrix A =


2 4 6
8 10 12
14 16 18

You might also like