Add Two Matrices In C

The attached program in this blog explains how to add two matrices in C language by entering the order of the matrix. 

Software Requirements

  • Turbo C++ or C.

Programming

Here is the complete program to add a matrix in C.

#include < stdio.h >   
int main()  
{  
    int m, n, c, d, first[10][10], second[10][10], sum[10][10];  
    printf("Enter the number of rows and columns of matrix\n");  
    scanf("%d%d", & m, & n);  
    printf("Enter the elements of first matrix\n");  
    for (c = 0; c < m; c++)  
        for (d = 0; d < n; d++) scanf("%d", & first[c][d]);  
    printf("Enter the elements of second matrix\n");  
    for (c = 0; c < m; c++)  
        for (d = 0; d < n; d++) scanf("%d", & second[c][d]);  
    printf("Sum of entered matrices:-\n");  
    for (c = 0; c < m; c++)  
    {  
        for (d = 0; d < n; d++)  
        {  
            sum[c][d] = first[c][d] + second[c][d];  
            printf("%d\t", sum[c][d]);  
        }  
        printf("\n");  
    }  
    return 0;  
}

Explanation

The above program creates the first and second two matrices with dimensions [10,10] each. The sum[10,10] is the third matrix that stores the sum of the two matrices. The program loops through two matrices, get their elements and adds them, and stores them in the third matrix.

The sum of two matrices is printed on the screen successfully.

Output

add matrix in C