Matrix addition is the process of adding two matrices by adding their corresponding elements. In C, matrix addition can be performed using two-dimensional arrays, nested loops, and functions. Two matrices can be added only when they have the same number of rows and columns.
- Matrix addition involves adding elements at the same row and column positions.
- Both matrices must have the same dimensions for addition.
- The resultant matrix has the same number of rows and columns as the input matrices.

Example:
Consider two matrices A and B of order 2 × 2:
A=\begin{bmatrix}1&2\\3&4\end{bmatrix} \qquad B=\begin{bmatrix}5&6\\7&8\end{bmatrix} Adding their corresponding elements:
A+B=\begin{bmatrix}1+5&2+6\\3+7&4+8\end{bmatrix} The resultant matrix is:
C=\begin{bmatrix}6&8\\10&12\end{bmatrix} The general formula for matrix addition is:
C[i][j]=A[i][j]+B[i][j] Here, A[i][j] and B[i][j] represent the elements at row i and column j, while C[i][j] stores their sum.
Approaches to Add Two Matrices
Matrix addition can be implemented using different approaches depending on how the matrix elements are provided and how the addition logic is organized.
1. Program to Add Two Matrices Using Nested Loops
Nested loops are used to traverse two matrices and add their corresponding elements. The outer loop iterates through rows, while the inner loop iterates through columns.
Approach:
- Initialize two matrices A and B with the same dimensions.
- Declare a third matrix sum to store the result.
- Use an outer for loop to iterate through the rows.
- Use an inner for loop to iterate through the columns.
- Add corresponding elements using sum[i][j] = A[i][j] + B[i][j].
- Display the resultant matrix using nested loops.
#include <stdio.h>
int main(){
int A[2][2] = {{1, 2}, {3, 4}};
int B[2][2] = {{5, 6}, {7, 8}};
int sum[2][2];
// Add corresponding matrix elements
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++){
sum[i][j] = A[i][j] + B[i][j];
}
}
// Display the resultant matrix
printf("Sum of two matrices:\n");
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++){
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
Output
Sum of two matrices: 6 8 10 12
Explanation:
- Two matrices A and B are initialized with predefined values.
- Nested loops access each element using row and column indices.
- Corresponding elements are added and stored in the sum matrix.
- The resultant matrix is displayed using another pair of nested loops.
2. Program to Add Two Matrices Using User Input
This approach allows users to enter the matrix dimensions and elements at runtime. The program reads both matrices, adds their corresponding elements, and displays the result.
Approach:
- Read the number of rows and columns from the user.
- Validate that the entered dimensions are within the supported range.
- Declare three matrices A, B, and sum.
- Use nested loops to read the elements of the first and second matrices.
- Add corresponding elements and store the result in sum.
- Display the resultant matrix in row-column format.
#include <stdio.h>
int main(){
int rows, cols;
int A[10][10], B[10][10], sum[10][10];
// Read matrix dimensions
printf("Enter rows and columns: ");
if (scanf("%d %d", &rows, &cols) != 2 ||
rows < 1 || rows > 10 ||
cols < 1 || cols > 10) {
printf("Invalid matrix dimensions.\n");
return 1;
}
// Input first matrix
printf("Enter elements of first matrix:\n");
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++) {
if (scanf("%d", &A[i][j]) != 1)
return 1;
}
}
// Input second matrix
printf("Enter elements of second matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (scanf("%d", &B[i][j]) != 1)
return 1;
}
}
// Add both matrices
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum[i][j] = A[i][j] + B[i][j];
}
}
// Display resultant matrix
printf("Resultant Matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Enter rows and columns: 2 2
Enter elements of first matrix:
1 2
3 4
Enter elements of second matrix:
5 6
7 8
Resultant Matrix:
6 8
10 12
Explanation:
- The program accepts the number of rows and columns from the user.
- Nested loops read the elements of both matrices.
- Corresponding elements are added and stored in the sum array.
- The program prints the resultant matrix in the same dimensions as the input matrices.
3. Program to Add Two Matrices Using a Function
A separate function can perform matrix addition, allowing the addition logic to be reused without repeating the code in main().
Approach:
- Define the number of rows and columns using constants.
- Create a function addMatrices() that accepts two input matrices and a result matrix.
- Inside the function, use nested loops to traverse the matrix elements.
- Add corresponding elements and store the values in the result matrix.
- Initialize two matrices in the main() function.
- Call addMatrices() and display the resultant matrix.
#include <stdio.h>
#define ROWS 2
#define COLS 2
// Function to add two matrices
void addMatrices(int A[ROWS][COLS],
int B[ROWS][COLS],
int sum[ROWS][COLS]) {
for (int i = 0; i < ROWS; i++){
for (int j = 0; j < COLS; j++) {
sum[i][j] = A[i][j] + B[i][j];
}
}
}
int main(){
int A[ROWS][COLS] = {{1, 2}, {3, 4}};
int B[ROWS][COLS] = {{5, 6}, {7, 8}};
int sum[ROWS][COLS];
// Call the addition function
addMatrices(A, B, sum);
// Display the resultant matrix
printf("Sum of two matrices:\n");
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
Output
Sum of two matrices: 6 8 10 12
Explanation:
- The addMatrices() function accepts two input matrices and a result matrix.
- Nested loops add corresponding elements from both matrices.
- The calculated values are stored in the sum matrix.
- The main() function calls addMatrices() and displays the result.
Properties of Matrix Addition
Matrix addition follows several mathematical properties:
- Matrix addition is commutative: A+B=B+A.
- Matrix addition is associative: (A+B)+C=A+(B+C).
- Adding a zero matrix does not change the original matrix: A+0=A.
- Adding a matrix to its additive inverse produces a zero matrix: A+(-A)=0.
- Matrix addition is defined only for matrices with identical dimensions.