# C program to add two matrices

Category: C Program

Learn how to write a C program to add two matrices. This article provides a detailed explanation and sample code for matrix addition using a simple iterative approach.

Matrix addition is a fundamental operation in linear algebra with numerous applications in scientific computing, engineering, and computer graphics. This article will guide you through writing a C program to add two matrices, providing a detailed explanation and sample code.

## Steps to Add Two Matrices

1. Input the Matrices: Read the elements of the two matrices from the user.
2. Initialize the Result Matrix: Create a matrix to store the result of the addition.
3. Perform the Addition: Add corresponding elements of the two matrices and store the results in the result matrix.
4. Print the Result: Output the result matrix to the console.

## Write a C program to add two matrices

#include <stdio.h>

int main() {
int rows, cols;

// Input the dimensions of the matrices
printf("Enter the number of rows and columns -\n");
scanf("%d %d", &rows, &cols);

// Declare the matrices
int matrix1[rows][cols], matrix2[rows][cols], result[rows][cols];

// Input elements of the first matrix
printf("\nEnter elements of the first matrix -\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &matrix1[i][j]);
}
}

// Input elements of the second matrix
printf("\nEnter elements of the second matrix -\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &matrix2[i][j]);
}
}

for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

// Print the result matrix
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}

return 0;
}

Output

Enter the number of rows and columns -
3
3

Enter elements of the first matrix -
Element [0][0]: 1
Element [0][1]: 2
Element [0][2]: 3
Element [1][0]: 4
Element [1][1]: 5
Element [1][2]: 6
Element [2][0]: 7
Element [2][1]: 8
Element [2][2]: 9

Enter elements of the second matrix -
Element [0][0]: 9
Element [0][1]: 8
Element [0][2]: 7
Element [1][0]: 6
Element [1][1]: 5
Element [1][2]: 4
Element [2][0]: 3
Element [2][1]: 2
Element [2][2]: 1

10 10 10
10 10 10
10 10 10

### Explanation

1. Input the Matrices: The user inputs the dimensions (number of rows and columns) of the matrices and then enters the elements of the two matrices.
2. Initialize the Result Matrix: A result matrix of the same dimensions is declared to store the results of the addition.
3. Perform the Addition: Nested loops iterate over each element of the matrices, adding corresponding elements and storing the results in the result matrix.
4. Print the Result: The program prints the elements of the result matrix.

### Detailed Steps

• Step 1: Input the Matrices
• The program prompts the user to enter the number of rows and columns for the matrices.
• The user inputs the elements of the first and second matrices.
• Step 2: Initialize the Result Matrix
• A result matrix is declared to store the sum of corresponding elements from the first and second matrices.
• Step 3: Perform the Addition
• Two nested loops iterate through each element of the matrices.
• The corresponding elements of the two matrices are added, and the sum is stored in the result matrix.
• Step 4: Print the Result
• The program prints the elements of the result matrix row by row.