# C Program to Find Sum and Difference of Two Matrices

This is a C program used to calculate the sum & difference of the matrices.

Problem Description

This program calculates the sum & difference of the matrices. The program first reads 2 matrices and then performs both addition and subtraction of matrices.

Problem Solution

1. Create four 2D arrays (matrices), 2 for storing data, one for storing sum of the element of first two matrices, one for storing difference of first two matrices.
2. Define elements of the first two matrices.
3. Make function for reading elements of two matrices, printing elements of these two matrices and to calculate sum and difference of these matrices.
4. All the four operations involve a nested for loop, with two iterators i and j. Read element through scanf() and print element through printf().
5. Using both iterators we will locate each position of both arrays, take sum of elements of the array occurring at the same position and storing the sum in the third array at the same location. Same step to be followed for difference.

Program/Source Code

Here is source code of the C program to calculates the sum & difference of the matrices. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

1. `    /*`
2. `     * C program to accept two matrices and find the sum`
3. `     * and difference of the matrices`
4. `     */`
5. ` `
6. `    #include <stdio.h>`
7. `    #include <stdlib.h>`
8. ` `
9. `    void readmatA();`
10. `    void printmatA();`
11. `    void readmatB();`
12. `    void printmatB();`
13. ` `
14. `    void sum();`
15. `    void diff();`
16. ` `
17. `    int a[10][10], b[10][10], sumarray[10][10], arraydiff[10][10];`
18. `    int i, j, row1, column1, row2, column2;`
19. ` `
20. `    void main()`
21. `    {`
22. ` `
23. `        printf("Enter the order of the matrix A \n");`
24. `        scanf("%d %d", &row1, &column1);`
25. ` `
26. `        printf("Enter the order of the matrix B \n");`
27. `        scanf("%d %d", &row2, &column2);`
28. ` `
29. `        if (row1 != row2 && column1 != column2)`
30. `        {`
31. `            printf("Addition and subtraction are possible \n");`
32. `            exit(1);`
33. `        }`
34. ` `
35. `        else `
36. `        {`
37. ` `
38. `            printf("Enter the elements of matrix A \n");`
39. `            readmatA();`
40. ` `
41. `            printf("MATRIX A is \n");`
42. `            printmatA();`
43. ` `
44. `            printf("Enter the elements of matrix B \n");`
45. `            readmatB();`
46. ` `
47. `            printf("MATRIX B is \n");`
48. `            printmatB();`
49. ` `
50. `            sum();`
51. `            diff();`
52. ` `
53. `        }`
54. ` `
55. `    }`
56. ` `
57. `    /*  Function to read a matrix A */`
58. ` `
59. `    void readmatA() `
60. `    {`
61. `        for (i = 0; i < row1; i++) `
62. `        {`
63. `            for (j = 0; j < column1; j++)`
64. `            {`
65. `                scanf("%d", &a[i][j]);`
66. `            }`
67. `        }`
68. `        return;`
69. `    }`
70. ` `
71. `    /*  Function to read a matrix B */`
72. ` `
73. `    void readmatB() `
74. `    {`
75. `        for (i = 0; i < row2; i++) `
76. `        {`
77. `            for (j = 0; j < column2; j++) `
78. `            {`
79. `                scanf("%d", &b[i][j]);`
80. `            }`
81. `        }`
82. `    }`
83. ` `
84. `    /*  Function to print a matrix A */`
85. ` `
86. `    void printmatA()`
87. `    {`
88. `        for (i = 0; i < row1; i++) `
89. `        {`
90. `            for (j = 0; j < column1; j++)`
91. `            {`
92. `                printf("%3d", a[i][j]);`
93. `            }`
94. `            printf("\n");`
95. `        }`
96. ` `
97. `    }`
98. ` `
99. `    /*  Function to print a matrix B */`
100. ` `
101. `    void printmatB() `
102. `    {`
103. `        for (i = 0; i < row2; i++) `
104. `        {`
105. `            for (j = 0; j < column2; j++)`
106. `            {`
107. `                printf("%3d", b[i][j]);`
108. `            }`
109. `            printf("\n");`
110. `        }`
111. ` `
112. `    }`
113. ` `
114. `    /*  Function to do the sum of elements of matrix A and Matrix B */`
115. ` `
116. `    void sum() `
117. `    {`
118. `        for (i = 0; i < row1; i++)`
119. `        {`
120. `            for (j = 0; j < column2; j++) `
121. `            {`
122. `                sumarray[i][j] = a[i][j] + b[i][j];`
123. `            }`
124. ` `
125. `        }`
126. ` `
127. `        printf("Sum matrix is \n");`
128. `        for (i = 0; i < row1; i++)`
129. `        {`
130. `            for (j = 0; j < column2; j++)`
131. `            {`
132. `                printf("%3d", sumarray[i][j]) ;`
133. `            }`
134. `            printf("\n");`
135. ` `
136. `        }`
137. `        return;`
138. ` `
139. `    }`
140. ` `
141. `    /*  Function to do the difference of elements of matrix A and Matrix B */`
142. ` `
143. `    void diff()`
144. `    {`
145. `        for (i = 0; i < row1; i++)`
146. `        {`
147. `            for (j = 0; j < column2; j++)`
148. `            {`
149. `                arraydiff[i][j] = a[i][j] - b[i][j];`
150. `            }`
151. ` `
152. `        }`
153. ` `
154. `        printf("Difference matrix is \n");`
155. `        for (i = 0; i < row1; i++)`
156. `        {`
157. `            for (j = 0; j < column2; j++)`
158. `            {`
159. `                printf("%3d", arraydiff[i][j]);`
160. ` `
161. `            }`
162. `            printf("\n");`
163. ` `
164. `        }`
165. `        return;`
166. ` `
167. `    }`
Program Explanation

1. Declare four 2D array of some fixed capacity. 2 for storing data, one for storing sum of the elements of first two matrices, one for storing difference of first two matrices.
2. Take the number of rows and columns as input from users. Accordingly define the elements of first two arrays.
3. For reading and printing the matrices, make separate functions and use scanf() and printf() respectively inside nested for loop.
4. For evaluating sum and difference, make a nested loop with 2 iterators i and j, where i will track the row of array and j will track the column of the array.
5. Inside loop, elements from both the array from each location are added and the sum is stored at the same location of the third 2D array.
6. For evaluating difference, same step will be followed as above except difference of the elements will be stored in the 4th 2D array.
7. Print the sum and difference matrices.

Runtime Test Cases
```Enter the order of the matrix A
3 3
Enter the order of the matrix B
3 3
Enter the elements of matrix A
1 4 5
6 7 8
4 8 9
MATRIX A is
1  4  5
6  7  8
4  8  9
Enter the elements of matrix B
3 6 7
8 4 2
1 5 3
MATRIX B is
3  6  7
8  4  2
1  5  3
Sum matrix is
4 10 12
14 11 10
5 13 12
Difference matrix is
-2 -2 -2
-2  3  6
3  3  6```

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!