C Program for Matrix Addition

In this article, C Program for matrix addition is discussed, thereby explaining the concept behind addition of matrix and implementing it later in C Programming Language. The dry run, algorithm and analysis are also discussed in this article to give the student a strong conceptual understanding of the topic.

About Matrices

Matrices are rectangular arrays consisting of numbers, symbols, variables or expressions, where horizontal lines are termed as rows and vertical lines are termed as columns. Matrices are extensively used in programming to solve complex problems related to graphs, linear equations, image processing, statistics etc.

Operations like addition, subtraction and multiplication are prevalent in solving problems and in this article, matrix addition along with c program for matrix addition is given in detail.

Dry Run of Matrix Addition

Suppose there are two matrices, namely A and B where Matrix A is [[2,4],[3,9]]. On the other hand, matrix B consists of the values [[3,5],[3,9]]. In order to add the matrix, there must be a point to keep in mind if the number of rows and columns or dimension of matrix share the same value in both the matrices or not as inequality of matrix dimension cannot make matrix addition possible.

To add the matrices, we start from coordinates (0,0) and traverse until (n-1,m-1) of both the matrices where n is the total number of rows and m is the total number of columns. We maintain a storage matrix to store the result from addition operations.

We add the elements at row 0 and column 0 i.e. 2+3 equals to 5.

We add the elements at row 0 and column 1 i.e. 4+5 equals to 9.

We add the elements at row, 1 and column,0 i.e. 3+3 equals to 6.

We add the elements at row, 1 and column,1 i.e. 9+9 equals to 18.

In this manner, we get matrix C as the result with added values from both matrix A and matrix B.

Algorithm of Matrix Addition in C

The dry run explains each sub-operation chronologically for the addition of matrices until we receive an output matrix with added values from both the matrices. Let us look at the algorithm for c program for matrix addition to understand in a step by step manner on how the problem is countered.

  1. Take two matrices having same dimensions as input from the user
  2. Initialize a matrix to store the results having the same dimension as the input matrices
  3. Add the elements from input matrices at corresponding positions and store them in the storage array at the similar position
  4. Print the storage array

Matrix Addition Program in C

Now that we have understood how the program works using the dry run approach and logic behind the program with the help of the algorithm discussed in the above section, in this section, implementation of the c program for matrix addition is performed.

#include <stdio.h>

#define M 10
#define N 10

int main()
    int m, n, c, d, one[M][N], two[M][N], three[M][N];
    printf("Enter the number of rows and columns of matrix: ");
    scanf("%d%d", &m, &n);
    printf("Enter the elements of first matrix: ");
    for (c = 0; c < m; c++)
        for (d = 0; d < n; d++) scanf("%d", &one[c][d]);
    printf("Enter the elements of second matrix: ");
    for (c = 0; c < m; c++)
        for (d = 0; d < n; d++) scanf("%d", &two[c][d]);
    printf("Sum of entered matrices:\n");
    for (c = 0; c < m; c++)
        for (d = 0; d < n; d++)
            three[c][d] = one[c][d] + two[c][d];
            printf("%d\t", three[c][d]);
    return 0;


As we received the input from the user, a storage array is created to keep all of obtained results

A loop is initialized with int i=0 and runs till n-1 (number of rows) and a nested loop initialized with j=0 that runs till m-1 (number of columns), inside the loop addition operation is performed where elements at ith row and jth column from both the matrices are summed to store in the resultant matrix.

Analysis of Program

The time complexity of the program will jump up to O(N^2) for a matrix with equal rows and columns and O(M*N) for a matrix with differing rows and columns and the reason being that each row is going to be traversed column times and vice versa.

The space complexity is going to be O(N^2) for matrices with equal rows and columns as auxiliary space will be required for all the rows as well as columns. In clear words, an entire dummy matrix will be created for the purpose of storing results.


In this article focused on the c program for matrix addition, we understood what a matrix is and how addition can be performed on a matrix. Later, we moved to the code implementation along with the dry run algorithm and analyzed the complexity of the code for the space and time it takes in the worst case.

Other C Programs

C Program for Binary Search
C Program to Add Two Numbers
C Program to Calculate Percentage of 5 Subjects
C Program to Convert Binary Number to Decimal Number
C Program to Convert Celsius to Fahrenheit
C Program to Convert Infix to Postfix
C Program to Find Area of Circle
C Program to Find Roots of Quadratic Equation
C program to Reverse a Linked List
C program to reverse a number
Ascending Order Program in C
Menu Driven Program For All Operations On Doubly Linked List in C
C Program for Armstrong Number
C Program For Merge Sort For Linked Lists
C program for performing Bubble sort on Linked List
Hello World Program in C
Perfect Number Program in C
Leap Year Program in C
Odd Even Program in C
Selection Sort Program in C
Linear Search Program in C
While Loop Program in C
C Program to Swap Two Numbers
Calculator Program in C Language
Simple Interest Program in C
Compound Interest Program in C
Priority Scheduling Program in C
Doubly Linked List Program in C
FCFS Scheduling Program in C
Insertion Sort Program in C
Singly Linked List Program in C
Star Program in C
String Program in C
Radix Sort Program in C

Leave a Reply

Your email address will not be published. Required fields are marked *