✓ Link copied to clipboard
C 📅 July 10, 2026 ⏱ 6 min read

C Program to Add Two Matrices Using Multi-dimensional Arrays

C Program to Add Two Matrices Using Multi-dimensional Arrays

Matrix addition's a common thing in math and engineering. If you've got two matrices and wanna add them element by element, this program shows you how to do it in C. For instance, if Matrix A is [[1, 2], [3, 4]] while Matrix B is [[5, 6], [7, 8]], the result will be [[6, 8], [10, 12]].

Let's dive into adding two matrices using multi-dimensional arrays.

Prerequisites for Adding Matrices in C

Before writing the matrix addition program in C, make sure you know the following:

  • Multi-dimensional arrays and their syntax in C.
  • The <stdio.h> library for input and output.
  • Nested loops for iterating through matrix elements.
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.

Source Code of C Program to Add Two Matrices

Here's the complete code to add two matrices using multi-dimensional arrays in C.

add_matrices.cC▶ Run
#include <stdio.h>

int main() {
    int a[10][10], b[10][10], c[10][10];
    int m, n, i, j;
    
    // Input dimensions
    printf("Enter the number of rows (max 10): ");
    scanf("%d", &m);
    printf("Enter the number of columns (max 10): ");
    scanf("%d", &n);
    
    // Input elements of first matrix
    printf("Enter elements of matrix A:\n");
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            printf("Enter element A[%d][%d]: ", i, j);
            scanf("%d", &a[i][j]);
        }
    }
    
    // Input elements of second matrix
    printf("Enter elements of matrix B:\n");
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            printf("Enter element B[%d][%d]: ", i, j);
            scanf("%d", &b[i][j]);
        }
    }
    
    // Adding matrices
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            c[i][j] = a[i][j] + b[i][j];
        }
    }
    
    // Displaying the result
    printf("Sum of matrices A and B:\n");
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            printf("%d\t", c[i][j]);
        }
        printf("\n");
    }
    
    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Add Two Matrices Program

Below is a sample run of the program to add two matrices. Enter values for the matrices when prompted.

OutputTerminal
Enter the number of rows (max 10): 2
Enter the number of columns (max 10): 2
Enter elements of matrix A:
Enter element A[0][0]: 1
Enter element A[0][1]: 2
Enter element A[1][0]: 3
Enter element A[1][1]: 4
Enter elements of matrix B:
Enter element B[0][0]: 5
Enter element B[0][1]: 6
Enter element B[1][0]: 7
Enter element B[1][1]: 8
Sum of matrices A and B:
6	8
10	12
Output of C Program to Add Two Matrices Using Multi-dimensional Arrays
Output of C Program to Add Two Matrices Using Multi-dimensional Arrays

Algorithm to Add Two Matrices in C

Matrix addition involves adding corresponding elements from two matrices. Here is the exact logic the program follows, step by step.

  1. Read the number of rows and columns for the matrices.
  2. Input elements for matrix A and matrix B.
  3. Initialize matrix C to store the sum of matrices A and B.
  4. For each element, add the corresponding elements from matrix A and B, and store the result in matrix C.
  5. Output the resulting matrix C.
flowchart TD
  A([Start]) --> B[/Read matrix A and B/]
  B --> C["Initialize matrix C as the result"]
  C --> D["For each row i = 0 to m"]
  D --> E["For each column j = 0 to n"]
  E --> F["C[i][j] = A[i][j] + B[i][j]"]
  F --> E
  E --> G["Display matrix C"]
  G --> H([End])
Flowchart: the algorithm step by step

How the C Program to Add Two Matrices Works

Let's break down the code to understand how it adds two matrices.

  • The program starts by including the <stdio.h> library which is needed for input-output functions.
  • Three 2D arrays a, b, and c are declared to hold the matrices.
  • The user is prompted to enter the dimensions of the matrices. These are stored in m and n.
  • Elements of matrix A and matrix B are entered using nested loops, displaying prompts like Enter element A[i][j].
  • A nested loop structure iterates over each matrix element to perform addition and stores the result in matrix c.
  • Finally, the program outputs the resulting matrix using another set of nested loops.
  • The program ends with a return 0 statement, indicating successful execution.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What is the main purpose of using multi-dimensional arrays in this program?

🎉Correct! Multi-dimensional arrays help store and manipulate matrix data efficiently.
🤔Not quite. Think about how data is stored for matrix operations.

How Fast Is the Matrix Addition in C?

Let's discuss the time and space complexity of this matrix addition program.

Time Complexity: O(m * n). Here, m is the number of rows and n is the number of columns in the matrices. The program uses two nested loops: for (i = 0; i < m; i++) and for (j = 0; j < n; j++), each iterating over all elements. For example, a 3x3 matrix will perform 9 operations.

Space Complexity: O(m * n). The program uses three matrices a, b, and c, each with m * n integers. Thus, space grows with the size of the input matrices.

⚠️

Common mistake:Mixing row and column indices when accessing matrix elements can cause unexpected results.

Key takeaway:Matrix addition using multi-dimensional arrays is simple and efficient, but always make sure that the matrices match in size.

FAQs About Adding Two Matrices in C

Here are some common questions and answers about matrix addition in C.

If the matrices aren't the same size, you can't add them directly since each element needs a match in the other matrix.
You use nested loops to go through each element of the 2D arrays, letting you add the matrices element-wise.
The code’s set to handle matrices up to 10x10. To work with bigger ones, just increase the sizes of the arrays a, b, and c.
For this program, you only need <stdio.h> for input and output. No extra libraries are required for basic matrix stuff.
To change the program for subtraction, swap the addition line c[i][j] = a[i][j] + b[i][j] to subtraction: c[i][j] = a[i][j] - b[i][j].

If you found this program helpful, check out these related programs:

Conclusion

Matrix addition in C with multi-dimensional arrays is simple once you get the hang of multi-dimensional loops. Try different sizes and values to really understand it. Just remember, both matrices need to be the same size for addition to work.

Ready for more? Test your skills with our C Programming Quiz, or check out your code in the online compiler.